all Python 3itertoolsterm
itertools
module
Building blocks for lazy iterators: chaining, slicing, grouping, permutations and endless counters.
Syntax
import itertools
The functions in itertools return lazy iterators: nothing is computed until the result is consumed, and it can be consumed only once, so a second list() gives an empty list. count and cycle never end, so bound them with islice or takewhile. A common mistake is groupby on unsorted data: it only merges adjacent equal keys.
Examples
import itertools
it = itertools.chain([1, 2], (3,))
print(list(it), list(it)) # → [1, 2, 3] [] — итератор одноразовый
print(list(itertools.islice(itertools.count(10), 3))) # → [10, 11, 12]
print([k for k, _ in itertools.groupby('aabaa')]) # → ['a', 'b', 'a']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy