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:

Lectures

  1. 01Aggregators✓
  2. 02Slicing Iterables✓
  3. 03Selecting and Filtering✓
  4. 04Infinite Iterators✓
  5. 05Chaining and Teeing Iterators✓
  6. 06Mapping and Reducing✓
  7. 07Zipping✓
  8. 08Grouping✓
  9. 09Combinatorics✓

Practice: quiz & exercises

12 quiz questions · 4 coding exercises

Go to practice →