Part 2 · Section 08
Iteration Tools
Once you know how iterators and generators work, you start writing the same small loops again and again: take the first n items, skip a header, stop when a condition fails, glue two streams together, walk two sequences in parallel, group consecutive rows, try every pair. The standard library already ships fast, well-tested, lazy versions of all of these, mostly in the itertools module. Learning them lets you replace a page of loop bookkeeping with one readable expression that works on any iterable, including infinite ones.
The recurring theme of the section: these tools consume iterators as they go. Knowing exactly when values are pulled, and what is left behind afterwards, is what separates correct pipelines from subtle bugs.
In this section you’ll learn:
- the aggregators
min,max,sum,anyandall, and how to combine them withmapor generator expressions - slicing any iterable with
islice, and splitting it into chunks withbatched(Python 3.12) - selecting values with
filter,filterfalse,takewhile,dropwhileandcompress - the infinite iterators
count,cycleandrepeat - joining iterables with
chain/chain.from_iterable, and “copying” an iterator withtee map,starmap,functools.reduceand running totals withaccumulatezip,zip_longest,zip(strict=True)andpairwise(Python 3.10)- grouping consecutive items with
groupby, and its sharp edges - Cartesian products, permutations and combinations, with a couple of probability examples