The itertools module is a standard library module that provides a toolkit of fast, memory-efficient iterator building blocks. Its functions produce iterators that compute values lazily, making them suitable for processing large or infinite data streams. itertools is inspired by functional programming languages (Haskell, SML) and is one of Python's most powerful modules.
Key functions include: infinite iterators (count(), cycle(), repeat()); terminating iterators (chain(), islice(), compress(), takewhile(), dropwhile(), groupby()); and combinatoric iterators (product(), permutations(), combinations(), combinations_with_replacement()). Each function returns an iterator, so they compose without materialising intermediate collections.
itertools functions are often combined into pipelines. For example, itertools.chain.from_iterable(itertools.combinations(items, r) for r in range(len(items)+1)) generates the power set of items. The itertools.groupby() function groups consecutive equal elements — a powerful tool when combined with sorted data. The related functools module provides complementary tools like reduce(), partial(), and lru_cache(). Together, itertools and functools form the core of Python's functional programming toolkit.
Discussed in:
- Chapter 17: Decorators, Generators, and Iterators — The itertools Module