Revolutionize Your Python Programming with These Life-Saving itertools Functions!

Jahidul Hasan Hemal
4 min readApr 18, 2023

--

AI Art using Midjourney

itertools is a Python module that provides a variety of tools for working with iterators and iterables. It contains several functions that can help make common programming tasks easier and more efficient.

One such function is zip_longest(), which is used to combine two or more iterables into a single iterable. In this blog post, we will explore the zip_longest() function, as well as a few other useful functions in the itertools module.

zip_longest()

The zip_longest() function is similar to the built-in zip() function, but it fills in missing values with a specified default value instead of raising a TypeError. This can be useful when you are combining iterables of different lengths, as it ensures that every element from each iterable is included in the resulting iterable.

Here is an example of how to use zip_longest() to merge two lists of different lengths:

l1 = [1, 2, 3]
l2 = [4, 5]
merged_list = [x for t in zip_longest(l1, l2) for x in t if x is not None]
print(merged_list)

Output:

[1, 4, 2, 5, 3]

In this code, we first import the zip_longest() function from the itertools module. We then create two lists of different lengths, l1 and l2. We use zip_longest() to combine the two lists into a single list, merged_list, and then use a list comprehension to flatten the resulting list.

permutations()

The permutations() function is used to generate all possible permutations of a given iterable. It takes an iterable as an argument and returns an iterator that generates all possible permutations of that iterable.

Here is an example of how to use permutations() to generate all possible permutations of a string:

s = 'abc'
perms = permutations(s)
for p in perms:
print(''.join(p))

Output:

abc
acb
bac
bca
cab
cba

In this code, we import the permutations() function from the itertools module. We then create a string, s, and use permutations() to generate all possible permutations of the string. We then loop through the resulting iterator and print each permutation.

combinations()

The combinations() function is used to generate all possible combinations of a given iterable. It takes an iterable and a length as arguments and returns an iterator that generates all possible combinations of that iterable of the given length.

Here is an example of how to use combinations() to generate all possible combinations of a list:

l = [1, 2, 3]
combs = combinations(l, 2)
for c in combs:
print(c)

Output:

(1, 2)
(1, 3)
(2, 3)

In this code, we import the combinations() function from the itertools module. We then create a list, l, and use combinations() to generate all possible combinations of the list with a length of 2. We then loop through the resulting iterator and print each combination.

chain()

The chain function can be used to combine multiple iterable objects into a single iterable object, without creating a new list. This can be useful if you want to loop over multiple lists or other iterables, but don't want to create a new list containing all of the elements.

l1 = [1, 2, 3]
l2 = [4, 5]
l3 = [6, 7, 8]
for x in chain(l1, l2, l3):
print(x)
# Output:
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8

cycle()

The cycle function can be used to create an iterator that endlessly repeats a sequence of values. This can be useful if you want to repeatedly apply a certain pattern or set of values to some other operation or function.

colors = ['red', 'green', 'blue']
for color in cycle(colors):
print(color)
# Output:
# red
# green
# blue
# red
# green
# blue
# ...

islice()

The islice function can be used to slice an iterable object, without converting it to a list. This can be useful if you want to extract a certain portion of an iterable object, but don't want to create a new list containing all of the elements.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in islice(numbers, 2, 8):
print(number)
# Output:
# 3
# 4
# 5
# 6
# 7
# 8

The itertools module provides a number of useful functions for working with iterators. These functions can be used to simplify a variety of tasks, such as combining iterators, generating combinations or permutations of elements, and generating products of sequences.

I hope this blog has been helpful. If you have any questions, please feel free to leave a comment below.

--

--

Jahidul Hasan Hemal
Jahidul Hasan Hemal

Written by Jahidul Hasan Hemal

A goddamn marvel of modern science. An open-source enthusiast and an optimist who loves to read and watch movies and is trying to learn how to write.

No responses yet