Itertools is a Python module of functions that return generators, which are objects that only function when iterated over.


The chain() function takes several iterators as arguments. It goes through each element of each passed iterable, then returns a single iterator with the contents of all passed iterators.

import itertools
list(itertools.chain([1, 2], [3, 4]))

# Output
# [1, 2, 3, 4]


The islice() function returns specific elements from the passed iterator.

It takes the same arguments as the slice() operator for lists: start, stop, and step. Start and stop are optional.

import itertools
list(itertools.islice(count(), 5))

# Output
# [0, 1, 2, 3, 4]


izip() returns an iterator that combines the elements of the passed iterators into tuples.

It works similarly to zip(), but returns an iterator instead of a list.

import itertools
list(izip([1, 2, 3], ['a', 'b', 'c']))

# Output
# [(1, 'a'),(2, 'b'),(3, 'c')]

More Information: