`itertools` — Iterator Building Blocks
Python · Standard Library · Run with python3
Explanation
`itertools` provides memory-efficient combinatoric and functional iterators. Infinite: `count(start, step)`, `cycle(iterable)`, `repeat(obj, n)`. Finite: `islice`, `takewhile`, `dropwhile`, `filterfalse`, `compress`. Combinatoric: `product`, `permutations`, `combinations`, `combinations_with_replacement`. Grouping: `groupby`. Chaining: `chain`, `chain.from_iterable`. All are lazy (return iterators, not lists).
Real-World Scenario
Generating all possible parameter combinations for a grid search: `itertools.product([0.01, 0.1], [32, 64, 128], [True, False])` gives the Cartesian product of learning rates, batch sizes, and dropout flags — exactly what a hyperparameter sweep needs. ```python import itertools # Groupby — consecutive groups (data must be sorted first) data = [("A", 1), ("A", 2), ("B", 3), ("B", 4), ("A", 5)] data.sort(key=lambda x: x[0]) for key, group in itertools.groupby(data, key=lambda x: x[0]): print(f"{key}: {[v for _, v in group]}") # A: [1, 2, 5] B: [3, 4] # Batching with islice def batched(iterable, n): it = iter(iterable) while batch := list(itertools.islice(it, n)): yield batch for batch in batched(range(10), 3): print(batch) # [0, 1, 2] [3, 4, 5] [6, 7, 8] [9] # Combinatoric pairs = list(itertools.combinations("ABCD", 2)) print(f"{len(pairs)} pairs: {pairs[:3]}") ```
Quick Snippet
import itertools
# Groupby — consecutive groups (data must be sorted first)
data = [("A", 1), ("A", 2), ("B", 3), ("B", 4), ("A", 5)]
data.sort(key=lambda x: x[0])
for key, group in itertools.groupby(data, key=lambda x: x[0]):
print(f"{key}: {[v for _, v in group]}")
# A: [1, 2, 5] B: [3, 4]
# Batching with islice
def batched(iterable, n):
it = iter(iterable)
while batch := list(itertools.islice(it, n)):
yield batch
for batch in batched(range(10), 3):
print(batch)
# [0, 1, 2] [3, 4, 5] [6, 7, 8] [9]
# Combinatoric
pairs = list(itertools.combinations("ABCD", 2))
print(f"{len(pairs)} pairs: {pairs[:3]}")Full Example
# Compile: python3 itertools_basics.py
# Output:
# A: [1, 2]
# B: [3, 4]
# A: [5]
import itertools
data = [("A", 1), ("A", 2), ("B", 3), ("B", 4), ("A", 5)]
data.sort(key=lambda x: x[0])
for key, group in itertools.groupby(data, key=lambda x: x[0]):
print(f"{key}: {[v for _, v in group]}")
def batched(iterable, n):
it = iter(iterable)
while batch := list(itertools.islice(it, n)):
yield batch
for batch in batched(range(10), 3):
print(batch)
pairs = list(itertools.combinations("ABCD", 2))
print(f"{len(pairs)} pairs: {pairs[:3]}")
# chain — concatenate iterables
combined = list(itertools.chain([1, 2], [3, 4], [5]))
print(combined)
# product — Cartesian product
params = list(itertools.product([0.01, 0.1], ["sgd", "adam"]))
print(params)
# takewhile / dropwhile
print(list(itertools.takewhile(lambda x: x < 5, range(10))))
print(list(itertools.dropwhile(lambda x: x < 5, range(10))))
python3 itertools_basics.py