Writing the code for such common cases can be tedious and error-prone. Hence, developers have built libraries to do all this for you. You can use these functions by importing Itertools. In this article, you’ll learn about the Itertools module in Python and its functions.

What Is the Itertools Module?

The official Python documentation explains that Itertools contains code for building iterators. This module provides fast and efficient functions to work with lists and arrays.

Before using this module, you need to import it using the following syntax:

There are three different types of iterators present in this module.

Infinite iterators Combinatoric iterators Terminating iterators

Infinite Iterators

Infinite iterators can run a loop infinitely. These functions more often run using a for loop. There are three infinite iterators.

1. count(start, step)

The count() function takes two parameters: the start and the step. The loop begins from the start value and returns values that increment by step, which defaults to 1. Consider the example given below: the loop starts from 2 and will add 2 each time. The loop breaks when the value of i becomes 10.

Output:

2. repeat(number, timesToRepeat)

The repeat() function accepts two parameters. The first is a value that the function produces repeatedly. The second parameter is the number of times the number should repeat. If you don’t specify the second parameter, the loop will run infinitely.

Output:

3. cycle(input)

The cycle() function iterates through the input and prints individual items in a given order. When it reaches the end of its input, cycle restarts from the beginning.

Output:

Combinatoric iterators

The combinatoric iterators provide functions to perform permutations, combinations, and cartesian products.

1. product(input)

The product() function computes the cartesian product of the specified input. It has a repeat parameter that computes the cartesian product of an iterator with itself. It is an optional parameter.

Output:

2. permutations(input, size)

This function returns a tuple of all the permutations of the given iterable. It accepts two parameters: the iterable and group size. If the group size is not specified, it will form groups of the same length as the iterable itself.

Output:

3. combinations(input, length)

The combinations() function helps to compute the combinations of the given iterator. Note that this function maintains the item order of its input. While permutations includes values that differ only by order, combinations produces unique values.

Output:

Terminating iterators

Terminating iterators produce output based on the conditions given to the input. You can understand it best from some example functions.

1. accumulate(input, operator)

The accumulate() function accepts two arguments: the iterable and an operator. It produces output by applying the operator to a cumulative total and each input element in turn. The operator is an optional argument. If you don’t pass it, this function will perform addition.

Output:

2. starmap(function, input)

The starmap() function accepts a function and tuple list as its arguments. It computes return values by applying the function to each tuple in the input. In the example given, this function will compute the maximum value of each tuple and return it in an array.

Output:

3. filterfalse(function)

The filterfalse() function returns values that don’t meet the condition in the passed function. The code given below removes all the odd values.

Output:

Continue Your Python Coding Journey

Now that you have learned about the Itertools module, it’s time to explore other Python modules. Python has modules and libraries for a wide set of tasks. They include advanced concepts like Fourier transformation and AI/ML applications.

Some modules are more complicated than others and will take longer to learn. The Tkinter module enables you to build full GUI applications in Python.