Python itertools

Filed Under: Python

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:


itertools.chain(*sequence)

Let’s put this in an example:


from itertools import *

for value in chain([12.3, 2.5, 34.13], ['JournalDev', 'Python', 'Java']):
    print(value)

The output will be:
python itertools chain

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:


from itertools import *

for num in islice(count(), 4):
    print(num)
print('I stopped at 4.')

for num in islice(count(), 15, 20):
    print(num)
print('I started at 15 and stopped at 20.')

for num in islice(count(), 5, 50, 10):
    print(num)
print('I started at 5 and leaped by 10 till 50.')

The output will be:
python itertools islice count functions

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:


from itertools import *

single_iterator = islice(count(), 3)
cloned1, cloned2 = tee(single_iterator)

for num in cloned1:
    print('cloned1: {}'.format(num))
for num in cloned2:
    print('cloned2: {}'.format(num))

The output will be:
python itertools tee

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:


itertools.cycle(sequence)

Let’s provide a sample code snippet:


from itertools import *

index = 0
for item in cycle(['Python', 'Java', 'Ruby']):
    index += 1
    if index == 12:
        break
    print(index, item)

The output will be:
python itertools cycle

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:


from itertools import *

data = accumulate(range(10))
for item in data:
    print(item)

The output will be:
python itertools accumulate sum

Let’s try another operator like multiplication as:


from itertools import *
import operator

data = accumulate(range(1, 5), operator.mul)
for item in data:
    print(item)

The output will be:
python itertools accumulate multiplication

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:


itertools.dropwhile(predicate, sequence)

Let’s provide a sample code snippet:


from itertools import *

data = dropwhile(lambda x: x < 5, [3, 12, 7, 1, -5])
for item in data:
    print(item)

The output will be:
python itertools dropwhile

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:


itertools.takewhile(predicate, sequence)

Let’s put this in an example:


from itertools import *

data = takewhile(lambda x: x < 5, [3, 12, 7, 1, -5])
for item in data:
    print(item)

The output will be:
python itertools takewhile

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:


itertools.combinations(sequence, r)

Let’s provide a sample code snippet:


from itertools import *

data = list(combinations('ABCD', 2))
for item in data:
    print(item)

The output will be:
python itertools combinations
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:


itertools.combinations_with_replacement(sequence, r)

Let’s put this in an example:


from itertools import *

data = list(combinations_with_replacement('ABCD', 2))
for item in data:
    print(item)

The output will be:
python itertools combinations repetitions

Compression filter with compress()

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

Syntax for compress works as:


itertools.compress(sequence, selector)

Let’s provide a sample code snippet:


from itertools import *

filtered = [True, False, False, True, True]
to_filter = 'PQRSTUVW'
data = list(compress(to_filter, filtered))
for item in data:
    print(item)

The output will be:
python itertools compress

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

Reference: API Doc

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages