Learn itertools

Fast, memory-efficient building blocks for working with iterators

13 lessons · 2 hours · 4 levels · covers itertools Python 3.13

itertools provides lazy iterator functions implemented in C: infinite counters, chaining, slicing, grouping, running totals, batching and combinatorics. Combined, they express data-processing pipelines that handle huge or endless streams in constant memory. Knowing them replaces many hand-written loops with short, fast and correct code.

Before you start

itertools builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 29 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 59 min in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The itertools tree

itertoolsLazy iterators and itertoolsLazy iterators anditertoolscount, cycle and repeatcount, cycle andrepeatChaining iterables with chainChaining iterableswith chainSlicing iterators with isliceSlicing iterators withisliceproduct, permutations and combinationsproduct,permutations and c…Grouping consecutive items with groupbyGroupingconsecutive items…accumulate, pairwise and batchedaccumulate, pairwiseand batchedIterators and generatorsIterators andgeneratorsTuples and unpackingTuples andunpackingDefining functionsDefining functions
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Lazy iteration

  1. Lazy iterators and itertools · Essentials · 8 min

    Understand what itertools functions return, why they are lazy, and why an iterator can be consumed only once.

  2. count, cycle and repeat · Essentials · 7 min

    Generate endless sequences with count, cycle and repeat, and stop them safely.

  3. Exhaustion, StopIteration and laziness traps · Expert · 11 min

    Diagnose the subtle bugs of iterator code: silent exhaustion, StopIteration inside generators, late binding and stale views.

Combining and slicing

  1. Chaining iterables with chain · Essentials · 7 min

    Iterate over several iterables as one with chain and flatten one level of nesting with chain.from_iterable.

  2. Slicing iterators with islice · Essentials · 7 min

    Take, skip and step through any iterator with islice, the lazy equivalent of slicing.

Combinatorics

  1. product, permutations and combinations · Practical · 10 min

    Generate Cartesian products, orderings and subsets lazily with product, permutations, combinations and combinations_with_replacement.

Grouping and aggregating

  1. Grouping consecutive items with groupby · Practical · 10 min

    Group consecutive items that share a key with groupby, and know why you usually sort first.

  2. accumulate, pairwise and batched · Practical · 10 min

    Compute running totals with accumulate, look at neighbouring pairs with pairwise, and chunk streams with batched.

Pipelines and recipes

  1. takewhile, dropwhile, compress and starmap · Advanced · 10 min

    Use takewhile, dropwhile, filterfalse, compress, starmap and zip_longest to filter and transform streams declaratively.

  2. tee and sharing one iterator · Advanced · 10 min

    Split one iterator into several independent ones with tee, and understand its memory cost and pitfalls.

  3. Lazy data pipelines · Advanced · 12 min

    Chain generators and itertools into streaming pipelines that process large files in constant memory.

  4. itertools recipes and more-itertools · Expert · 11 min

    Learn the standard recipes from the docs (sliding_window, roundrobin, unique_everseen, partition) and how they compose the primitives.

  5. Iterator performance in practice · Expert · 10 min

    Know when itertools and map beat comprehensions, how to measure iterator pipelines, and which overheads dominate.