Menu Close

Python itertools Module Tutorial

Python itertools Module

In this article, you will learn all about Python itertools module to work on iterators to produce complex iterators.itertools in Python provides various functions to work generate more complex iterators. Since we have seen simple iterators that are used to generate the next item of the elements.

Before going through this article, will demonstrate to you all about iterator with the help of the examples.

Before going through this article, will demonstrate to you all about iterator with the help of the examples.

What is an iterator in Python?

An iterator in Python is an object that contains a countable number of values. An iterator object gives the next value of the iterables when it is passed into next() function.

An iterable gives the iterator object when it passes into iter() function.
Let’s understand this by using an example.

Example


# list
lst = ['Python', 'Java', 'PHP', 'C++', 'C']

# create iterator object
iter = iter(lst)

# getting each item of the list lst
print(next(iter))
print(next(iter))

# you can also use __next__() function instead of next()
print(iter.__next__())
print(iter.__next__())

Output


Python
Java
PHP
C++

As you can in the above example, we have just defined a list lst, After we created an iter object using iter() function and getting the value of the lst using next() and __next__() function.

Note:- next() and __next()__ both works same.

I hope, you will be clear with the iterator in Python. Now it’s time to travel the Python itertools along with all the functionalities.

Python itertools Module

itertools module in Python is a built-in module which means we don’t need to install it by using the pip command, it comes with Python by default. Python itertools provides lots of functionality to generate more complex iterators which will discuss in this article.

Let’s understand all the Python itertools iterator functions one by one with the help of the examples.

Infinite Iterators

Python list, tuple, and set are iterable in Python. But it is not necessary that an iterator object has to exhaust. Sometimes our requirement is to create infinite iterators, in that case, the Python itertools module provides three different functions.

let’s understand them one by one.

count(start, step)

count() the function takes two parameters start and step. This function starts printing from the start till infinite. if a step provides then it will be skipped otherwise it takes the value of the step parameter to be 1 by default.

Example: Create infinite iterator using count()


from itertools import count
for i in count(5, 5):
    print(i, end=' ')
    if i == 50:
        break

Output

5 10 15 20 25 30 35 40 45 50

cycle(iterable)

This iterator is going to be used to print all the values in order from the passed container. It restarts printing from the beginning when all the items are printed in a cycle manner.

Example: Create infinite iterator using cycle()


from itertools import cycle
string = "Python"
count = 0
for i in cycle(string):
    if count <= 5:
        print(i, end=' ')
        count += 1
    else:
        break

Output

P y t h o n

repeat(value, num)

This function is used to print the passed value an infinite number of times. if the optional parameter is provided, then it repeatedly prints the nums number of times.

Example: Create infinite iterator using repeat()


from itertools import repeat

value = 50
print(list(repeat(value, 4)))

output

[50, 50, 50, 50]

Combinatoric Iterators

The recursive generator that is used to simply combinatorial constructs such as permutations, combinations, and cartesian products are called combinatoric iterators.
There are four types of combinatoric operators available in the Python itertools module.

product()

This iterator is used to calculate the cartesian product of the input values with itself. we use the optional repeat keyword argument to specify the number of iterations.

Example


from itertools import product

print("The cartesian product using repeat:- ", end=' ')
print(list(product([1, 2, 3], repeat=2)))

Output

The cartesian product using repeat:-  [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

permutations()

This function is used to generate all the possible permutations of the given iterable. This function takes two arguments first is iterable and the second is group_size, if group_size is not specified it treat the length of iterable as group_size.

Example


from itertools import permutations

print("Permutations of integer:- ")
print(list(permutations([1, 2])))

print("Permutations of string:- ")
print(list(permutations("Hi")))

Output


Permutations of integer:-
[(1, 2), (2, 1)]
Permutations of string:-
[('H', 'i'), ('i', 'H')]

Combinations()

This function is used to generate all the possible combinations without replacement.

Example


from itertools import combinations

print("Combinations of integer:- ")
print(list(combinations([1, 2], 2)))

print("Combinations of string:- ")
print(list(combinations("Hi", 2)))

Output


Combinations of integer:-
[(1, 2)]
Combinations of string:-
[('H', 'i')]

Terminating Iterators

terminating operators are used to work short sequence iterable and produce result or output based on the passed function as an argument.
There is various type of terminating operators available in itertools module in Python.

accumulate(iter, function)

This function takes two arguments first is iterable and the second is a function, and the function applies on each iteration of the value in the passed iterable. If any function does not provide it takes by default addition.

Example:


from itertools import accumulate
import operator

lst = [1, 2, 3, 4, 5]
print("Print the successive summation of the elements:- ")
print(list(accumulate(iterable=lst)))

print("Print the successive multiplication of the elements:- ")
print(list(accumulate(iterable=lst, func=operator.mul)))

Output


Print the successive summation of the elements:-
[1, 3, 6, 10, 15]
Print the successive multiplication of the elements:-
[1, 2, 6, 24, 120]

chain(iterable1, iterable2,..)

This function is used to print all the values of the passed iterables one after another.

Example


from itertools import chain

lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst3 = [7, 8, 9]

print("Print all the elements of above three list in one single list:- ", end=' ')
print(list(chain(lst1, lst2, lst3)))

Output

Print all the elements of above three list in one single list:-  [1, 2, 3, 4, 5, 6, 7, 8, 9]

chain.from_iterable()

This function works the same as chain but takes a list of lists or any other iterable as an argument.

Example


from itertools import chain

lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst3 = [7, 8, 9]

lst4 = [lst1, lst2, lst3]

print("Print all the elements of above three list in one single list:- ", end=' ')
print(list(chain.from_iterable(lst4)))

Output

Print all the elements of above three list in one single list:-  [1, 2, 3, 4, 5, 6, 7, 8, 9]

compress(iterable, selector)

This function picks the value to the print from the passed container based on the passed boolean value container list as another argument. The only items are printed when their corresponding boolean value will be true.

Example


from itertools import compress

var = "Python"
print(list(compress(data=var, selectors=[1, 0, 0, 1, 1, 1])))

Output

['P', 'h', 'o', 'n']

dropwhile(function, sequence)

This function is going to be used to print the values from the iterable when the specified function returns False for the first time.

Example


from itertools import dropwhile

def filterNow(i):
    if i % 2 == 0:
        return True
    else:
        return False


data = [10, 12, 3, 4, 5, 6, 7, 8, 9]
print(list(dropwhile(filterNow, data)))
print(list(dropwhile(lambda x: x % 2 == 0, data)))

Output


[3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]

filterfalse(function, iterable)

This function is used to print the value from the sequence when the specified function returns False.

Example


from itertools import filterfalse

def filterVowels(i):
    if i in ['i', 'e', 'o', 'u']:
        return True
    else:
        return False


name = "Python programming"
print(list(filterfalse(filterVowels, name)))

Output


['P', 'y', 't', 'h', 'n', ' ', 'p', 'r', 'g', 'r', 'a', 'm', 'm', 'n', 'g']

islice(iterable, starting position, ending position, step)

islice() function is used to print the selective element from the passed iterable.islice() function takes four arguments.

Example


from itertools import islice

data = [10, 12, 3, 4, 5, 6, 7, 8, 9]
print("The Result is:- ", list(islice(data, 2, 6, 1)))

Output

The Result is:-  [3, 4, 5, 6]

starmap(function, tuple list)

this iterator takes the function and list of tuples as an argument and returns value according to the passed function.

Example


from itertools import starmap

tpls = [(1,2,0), (12, 20, 10), (20, 24, 50), (100, 200, 400)]
print("The Result is:- ", list(starmap(min, tpls)))

Output

The Result is:-  [0, 10, 20, 100]

takewhile()

This function is just the opposite of the dropwhile() function. It prints the value from the iterable till specified function will return False.

Example


from itertools import takewhile 
def filterNow(i):
    if i % 2 == 0:
        return True
    else:
        return False


data = [10, 12, 3, 4, 5, 6, 7, 8, 9]
print(list(takewhile(filterNow, data)))
print(list(takewhile(lambda x: x % 2 == 0, data)))

Output


[10, 12]
[10, 12]

zip_longest()

This iterator is going to be print the value of the iterable alternatively in sequence. If one of the iterable full printed remaining values filled by the character is passed to fillvalue parameter.

Example


from itertools import zip_longest
iterable1 = "Python"
iterable2 = "Coding"
iterable3 = "Code"

print("The Result is:- ", list(zip_longest(iterable1, iterable2, iterable3, fillvalue="*")))

Output

The Result is:-  [('P', 'C', 'C'), ('y', 'o', 'o'), ('t', 'd', 'd'), ('h', 'i', 'e'), ('o', 'n', '*'), ('n', 'g', '*')]

Conclusion

In this article, you have learned all about Python itertools module that provides various functions to make more complex iterators.

Iterator in Python is nothing but it is an object that contains a countable number of values. It gives the next value of the iterable when it passed into the Python next() function.

If you like this article, please share and keep visiting for further Python tutorials.

Reference:- Click Here

~ Thanks for reading

What are Python Access modifiers?
Python class and instance variables

Related Posts