Overview of itertools¶
Let us go through one of the important library to manipulate collections called as itertools
.
- Functions such as
filter
andmap
are part of core Python libraries. reduce
is part offunctools
.- It is not possible to use these functions to perform advanced operations such as grouped aggregations, joins etc.
- Python have several higher level libraries which provide required functionality to perform advanced aggregations such as grouped aggregations, joins etc.
itertools
is one of the popular library to manipulate collections for such advanced operations.
In [1]:
import itertools as iter
In [2]:
itertools?
Object `itertools` not found.
In [3]:
iter.chain?
Init signature: iter.chain(self, /, *args, **kwargs) Docstring: chain(*iterables) --> chain object Return a chain object whose .__next__() method returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted. Type: type Subclasses:
In [4]:
l1 = [1, 2, 3, 4]
l2 = [4, 5, 6]
iter.chain(l1, l2)
Out[4]:
<itertools.chain at 0x7fc7c42842e0>
In [5]:
list(iter.chain(l1, l2))
Out[5]:
[1, 2, 3, 4, 4, 5, 6]