Python itertools With Examples

Python itertools module is very useful in creating efficient iterators. In almost every program you write with any programming language, one of the task which is usually always present is Iteration. Traversing sequence of objects and manipulating them is very common.

Many times while doing these common operations, we miss out on managing memory usage of the variables, size of the sequence being iterated and create a risk of inefficient code usage. With itertools module in Python, this can be prevented from happening with its functions.

Python itertools module

Python itertools module provide us various ways to manipulate the sequence while we are traversing it. Some of the most commons examples are shared here.

Python itertools chain()

Python itertools chain() function just accepts multiple iterable and return a single sequence as if all items belongs to that sequence.

Syntax for chain works as:

Let’s put this in an example:

The output will be:

itertools-chaining-1

It doesn’t matter if sequences passed were for different data type.

Automating sequences with islice() and count()

Itertools islice() and count() functions provide us an easy to make quick iterable and slice them through.

Let’s provide a sample code snippet:

The output will be:

itertools islice count-

Cloning sequences with tee()

The best way through which we can make a clone of a sequence is using tee() function. Remember that original sequence cannot be used once we have cloned into into 2 sequences.

Let’s put this in an example:

The output will be:

itertools islice count-

Cycling through sequences with cycle()

We can even iterate through a sequence has it was infinite. This works just like a circular Linked List.

Syntax for cycle works as:

Let’s provide a sample code snippet:

The output will be:

itertools islice count-

Accumulating operations with accumulate()

With accumulate() function, we can perform mathematical operations with a sequence and return the results. Like adding the numbers to the previous value in the sequence. Let’s put this in an example:

The output will be:

itertools islice count

Let’s try another operator like multiplication as:

The output will be:

itertools islice count

Filter items with dropwhile()

With dropwhile() function, we can filer sequence items until a condition becomes False. Once it becomes False, it stops the filter process.

Syntax for dropwhile works as:

Let’s provide a sample code snippet:

The output will be:

itertools-compress-1

Filter items with takewhile()

With takewhile() function, we can filer sequence items until a condition becomes True. Once it becomes True, it stops the filter process.

Syntax for takewhile works as:

Let’s put this in an example:

The output will be:

itertools-chaining-1

Making combinations with combinations()

When it comes to making combinations of all the values in a list, custom logic can go wrong in any number of ways. Even here, itertools module has a rescue function.

Syntax for combinations() works as:

Let’s provide a sample code snippet:

The output will be:
itertools-combinations-1
It is worth noticing that:

  • If items in sequence are sorted, than combination will be sorted as well.
  • If items in sequence are unique, than combination data will not contain any duplicate combination.

Repeated combinations with combinations_with_replacement()

This works just like the combinations() function as shown above. Only difference that this can have repeatitions in combination data.

Syntax for combinations_with_replacement works as:

Let’s put this in an example:

The output will be:

itertools-combinations-repetitions-1

Compression filter with compress()

Data compression is easy based on a Boolean list using the compress() function.

Syntax for compress works as:

Let’s provide a sample code snippet:

The output will be:

itertools-compress-1

In this lesson, we learned about various ways through which we can iterate and manipulate sequences with python itertools module.

Reference: API Doc

By admin

Leave a Reply