Python Itertools Module

In this article, we will learn about the itertools module The itertools module has multiple methods that make effective use of computing resources. We use these methods for generating different sorts of iterators. These iterators are employed in the construction of efficient loops. With the help of this module, we can greatly boost the maintainability and readability of our code. Let’s get started by importing the module.

Importing Python Itertools Module

To import the itertools module, we can use either the import keyword to import the entire module or both the from and import keyword to import a specific object from the module. Let us see an example of importing the itertools module.

Example of importing the itertools module in Python

import itertools

print(dir(itertools))

Output

[‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘_grouper’, ‘_tee’, ‘_tee_dataobject’, ‘accumulate’, ‘chain’, ‘combinations’, ‘combinations_with_replacement’, ‘compress’, ‘count’, ‘cycle’, ‘dropwhile’, ‘filterfalse’, ‘groupby’, ‘islice’, ‘permutations’, ‘product’, ‘repeat’, ‘starmap’, ‘takewhile’, ‘tee’, ‘zip_longest’]

In the above code, we imported the itertools module using the import keyword and used the function dir() to print a list of all objects in the itertools module.

Types of Iterators in Python

The functions in the itertools module can be categorized into three types depending on the type of iterator they create.

1. Infinite Iterators: The iterators that iterate infinitely are called Infinite Iterators.

2. Combinatoric Iterators: The recursive iterators that are used to simplify the difficult combinatorial operations are Combinatoric Iterators.

3. Terminating Iterators: The iterators that are commonly used to operate on small input sequences and create output based on the function passed to the iterator are called Terminating Iterators..

Infinite Iterators in Python

1. count(start=0, step=1)

The function takes two arguments: start and stop. The arguments can be integers or floats. If no arguments are passed, it takes 0 and 1 as the default values for start and step respectively. The function is used to print a sequence from start to infinite with step sized intervals.

Example of using count() in Python

import itertools

for i in itertools.count():
    print(i)
    if i == 9:
        break

Output

0
1
2
3
4
5
6
7
8
9

2. cycle(iterable)

The function takes an iterable as the argument and it is used to print all the elements in the passed iterable one by one. Once it prints the last element, it again starts printing from the first element in a cyclic method. This process continues infinitely.

Example of using cycle() in Python

import itertools

day = ["morning", "afternoon", "evening", "night"]

j = 1
for i in itertools.cycle(day):
    print(i)

    if j == 8:
        break

    j += 1

Output

morning
afternoon
evening
night
morning
afternoon
evening
night

3. repeat(object[, times])

The function takes two arguments. The first argument can be any object such as an integer, string or an iterable. The second argument is an integer and it is optional. The function prints the passed object for passed integer times. If no second argument is passed, it then prints the passed object infinitely.

Example of using repeat() in Python

import itertools

site = "PythonGeeks"

for i in itertools.repeat(site, 3):
    print(i)

Output

PythonGeeks
PythonGeeks
PythonGeeks

Combinatoric Iterators

1. product(*iterables, repeat=1)

The function is used to print the cartesian product of the two passed arguments.

Example of using product() in Python

import itertools

for i in itertools.product([3, 4], ['a', 'b']):
    print(i)

Output

(3, ‘a’)
(3, ‘b’)
(4, ‘a’)
(4, ‘b’)

2. permutations(iterable, r=None)

The function prints permutations of the passed iterable. The size of the permutation can be changed by passing an integer as the second argument.

Example of using permutations() in Python

import itertools

for i in itertools.permutations([1, 2, 3]):
    print(i)

Output

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

3. combinations(iterable, r)

The function prints r-sized combinations of the passed iterable without any replacements.

Example of using combinations() in Python

import itertools

for i in itertools.combinations([1, 2, 3], 2):
    print(i)

Output

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

4. combinations_with_replacement(iterable, r)

The function prints r-sized combinations of the passed iterable with replacements.

Example of using combinations() in Python

import itertools

for i in itertools.combinations_with_replacement([1, 2, 3], 2):
    print(i)

Output

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

Terminating Iterators in Python

1. accumulate(iterable[, func, *, initial=None])

The function takes two arguments, an iterable and a function. It passes the elements in the iterable to the passed function and prints the returned value of that passed function. If no function is passed, then addition is chosen as the default function.

Example of using accumulate() in Python

import itertools

for i in itertools.accumulate([1, 2, 3]):
    print(i)

Output

1
3
6

2. chain(*iterables)

The function prints the elements of the passed iterables one by one.

Example of using chain() in Python

import itertools

for i in itertools.chain([1, 2, 3], "Hi", (4, 5)):
    print(i)

Output

1
2
3
H
i
4
5

3. chain.from_iterable(iterable)

The function takes an iterable. The passed iterable must contain only iterables. The function prints the elements of the iterables present in the passed iterable. While the function chain() can take any number of iterables, the chain.from_iterable() function is limited to only one iterable.

Example of using chain() in Python

import itertools

for i in itertools.chain.from_iterable([[1, 2, 3], "Hi", (4, 5)]):
    print(i)

Output

1
2
3
H
i
4
5

4. compress(data, selectors)

The function can take two iterables as data and selector arguments. It prints only those elements in the data that have a matching element in selectors that evaluates to true. When the data or selected iterables are completed, it stops printing.

Example of using compress() in Python

import itertools

for i in itertools.compress(['a','b','c', 'd', 12], [1, 0, 1, 0, 1]):
    print(i)

Output

a
c
12<>/div

5. dropwhile(predicate, iterable)

The function has two parameters, predicate and iterable. It takes a function as the predicate and passes the elements from the iterable to the predicate one by one. It drops the elements as long as the predicate returns true. Once the predicate returns false, it starts printing the elements.

Example of using dropwhile() in Python

import itertools

for i in itertools.dropwhile(lambda x: x<7, [1,2,4,8,9,10,5,4,11,12]):
    print(i)

Output

8
9
10
5
4
11
12

6. filterfalse(predicate, iterable)

The function has two parameters, predicate and iterable. It takes a function as the predicate and passes the elements from the iterable to the predicate one by one. It prints only those elements that return false when passed to the predicate.

Example of using filterfalse() in Python

import itertools

for i in itertools.filterfalse(lambda x: x<7, [1,2,4,8,9,10,5,4,11,12]):
    print(i)

Output

8
9
10
11
12

7. groupby(iterable, key=None)

The function takes an iterable and a function as the arguments. If no function is passed or None is passed, thn it takes the identity function by default. The function returns an iterator that returns the iterable’s keys and groups in order. The groups that are returned are also iterators.

Example of using groupby() in Python

import itertools

for i in itertools.groupby([1, 1, 1, 2, 2, 1, 2, 1]):
    print(list(i[1]))

Output

1 [1, 1, 1]
2 [2, 2]
1 [1]
2 [2]
1 [1]

8. islice(iterable, start, stop[, step])

The function takes an iterable, a starting index, an ending index and a step integer as arguments. It prints the elements of the passed iterable from the starting index to the ending index.

Example of using islice() in Python

import itertools

for i in itertools.islice(['a','b','c','d','e','f'], 1, 4):
    print(i)

Output

b
c
d

9. starmap(function, iterable)

The function takes two arguments, a function and an iterable. It creates an iterator that passes the elements from the iterable to the passed function one by one and prints the result.

Example of using starmap() in Python

import itertools

def add(a, b):
    return a + b

for i in itertools.starmap(add, [[2, 3], [10, 5]]):
    print(i)

Output

5
15

10. takewhile(predicate, iterable)

This function is an inverse of the dropwhile() function. It has two parameters, predicate and iterable. This  takes a function as the predicate and passes the elements from the iterable to the predicate one by one. It drops the elements as long as the predicate returns false. Once the predicate returns true, it starts printing the elements.

Example of using takewhile() in Python

import itertools

for i in itertools.takewhile(lambda x: x<7, [1,2,4,8,9,10,5,4,11,12]):
    print(i)

Output

1
2
4

11. tee(iterable, n=2)

An iterator is divided into n distinct iterators using this function.

Example of using tee() in Python

import itertools

for i in itertools.tee([1,2,3, 4], 2):
    print(i)
    for j in i:
        print(j)

Output

<itertools._tee object at 0x10d0e3f80>
1
2
3
4
<itertools._tee object at 0x10d0e3dc0>
1
2
3
4

12. zip_longest(*iterables, fillvalue=None)

The function creates an iterator that combines elements from each iterable. If the iterables aren’t the same length, fillvalue is used to fill in the missing values.

Example of using zipl_longes() in Python

import itertools

for i in itertools.zip_longest([1,2,3], [4,5,6], [7, 8], [9], fillvalue=0):
    print(i)

Output

(1, 4, 7, 9)
(2, 5, 8, 0)
(3, 6, 0, 0)

Python Interview Questions on the itertools Module

Q1. Write a for-loop to print natural numbers infinitely.

Ans 1. Complete code is as follows:

from itertools import count

for i in count(1):
    print(i)

Output

1
2
3
KeyboardInterrupt

Q2. Write a for-loop to cycle through the list [1, 2, 3] infinitely.

Ans 2. Complete code is as follows:

from itertools import cycle

for i in cycle([1, 2, 3]):
    print(i)

Output

1
2
3
1
2
3
1
KeyboardInterrupt

Q3. Write a for-loop to print all possible three letter words that can be formed using the letters a, e, and t without repetitions.

Ans 3. Complete code is as follows:

from itertools import permutations

for i in permutations(['a', 'e', 't'], 3):
    print(i)

Output

(‘e’, ‘a’, ‘t’)
(‘e’, ‘t’, ‘a’)
(‘a’, ‘e’, ‘t’)
(‘a’, ‘t’, ‘e’)
(‘t’, ‘e’, ‘a’)
(‘t’, ‘a’, ‘e’)

Q4. Given two lists [‘a’, ‘b’], and [3, 2, 1] of unequal length, zip the two lists without using the zip() function and print the elements using a for-loop. Fill the missing values with character ‘#’.

Ans 4. Complete code is as follows:

from itertools import zip_longest

for i in zip_longest(['a', 'b'], [3, 2, 1], fillvalue='#'):
    print(i)

Output

(‘a’, 3)
(‘b’, 2)
(‘#’, 1)

Q5. Write a program using the accumulate() function found in the itertools module to print the factorial of the first 6 natural numbers.

Ans 5. Complete code is as follows:

from itertools import accumulate

def factorial(x, y):
    return x*y

for i in accumulate([1, 2, 3, 4, 5, 6], factorial):
    print(i)

Output

1
2
6
24
120
720

Conclusion

In this article, We learnt about the itertools module and how to use the various functions available in the module. In addition, if you have any queries, please feel free to leave them in the comments section.

Did you like our efforts? If Yes, please give PythonGeeks 5 Stars on Google | Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *