Code Reference

Itertools

Python · Reference cheat sheet

Itertools

Python · Reference cheat sheet


📋 Overview

itertools offers fast, memory-efficient building blocks for iterators: infinite streams, combinatorics, and chaining. Compose small tools instead of materializing huge lists.

🔧 Core concepts

FunctionRole
count cycle repeatInfinite / repeated
chain chain.from_iterableConcatenate
isliceSlice an iterator
teeSplit into n iterators
groupbyGroup consecutive keys
accumulateRunning totals
product permutations combinationsCombinatorics
zip_longestZip with fill
batchedChunks (3.12+)

All return iterators — consume with for, list, next, or islice.

💡 Examples

islice and count:

from itertools import count, islice

print(list(islice(count(10, 2), 5)))  # [10, 12, 14, 16, 18]

chain and groupby:

from itertools import chain, groupby

print(list(chain([1, 2], (3, 4))))

rows = [("a", 1), ("a", 2), ("b", 3)]
for key, group in groupby(rows, key=lambda r: r[0]):
    print(key, list(group))

Combinatorics:

from itertools import combinations, product

print(list(combinations("ABC", 2)))
print(list(product([0, 1], repeat=2)))

batched (3.12+) / chunk recipe:

from itertools import batched, islice
from collections.abc import Iterator

print(list(batched(range(10), 3)))

def chunked(it: Iterator[int], n: int) -> Iterator[list[int]]:
    while True:
        block = list(islice(it, n))
        if not block:
            return
        yield block

⚠️ Pitfalls

  • groupby requires sorted (or already consecutive) keys.
  • tee can buffer a lot if iterators diverge — prefer re-iterating sources.
  • Infinite iterators need a stop condition.
  • Combinatorial functions explode in size — bound inputs.
  • Exhausting one tee branch while another lags costs memory.

On this page