snippetpythonTippending
Python itertools recipes for efficient iteration
Viewed 0 times
itertoolschainbatchedcombinationsproductaccumulate
Problem
Common iteration patterns like batching, flattening, sliding windows require verbose manual code.
Solution
itertools provides memory-efficient building blocks:
from itertools import (
chain, islice, zip_longest, product,
combinations, permutations, groupby,
accumulate, repeat, cycle, starmap,
batched # Python 3.12+
)
# Flatten nested iterables
list(chain.from_iterable([[1,2], [3,4], [5]])) # [1,2,3,4,5]
# Batch/chunk (Python 3.12+)
list(batched(range(10), 3)) # [(0,1,2), (3,4,5), (6,7,8), (9,)]
# Sliding window (Python 3.10+ in more_itertools)
from itertools import pairwise # Python 3.10+
list(pairwise([1,2,3,4])) # [(1,2), (2,3), (3,4)]
# Cartesian product (replaces nested loops)
for x, y in product(range(3), range(3)):
print(x, y) # (0,0), (0,1), (0,2), (1,0)...
# Combinations and permutations
list(combinations('ABCD', 2)) # AB, AC, AD, BC, BD, CD
list(permutations('ABC', 2)) # AB, AC, BA, BC, CA, CB
# Running total
list(accumulate([1,2,3,4])) # [1, 3, 6, 10]
# Group consecutive items
for key, group in groupby('AAABBBCC'):
print(key, list(group)) # A [A,A,A], B [B,B,B], C [C,C]
# Take first N from generator (no list conversion)
first_10 = list(islice(infinite_generator(), 10))
from itertools import (
chain, islice, zip_longest, product,
combinations, permutations, groupby,
accumulate, repeat, cycle, starmap,
batched # Python 3.12+
)
# Flatten nested iterables
list(chain.from_iterable([[1,2], [3,4], [5]])) # [1,2,3,4,5]
# Batch/chunk (Python 3.12+)
list(batched(range(10), 3)) # [(0,1,2), (3,4,5), (6,7,8), (9,)]
# Sliding window (Python 3.10+ in more_itertools)
from itertools import pairwise # Python 3.10+
list(pairwise([1,2,3,4])) # [(1,2), (2,3), (3,4)]
# Cartesian product (replaces nested loops)
for x, y in product(range(3), range(3)):
print(x, y) # (0,0), (0,1), (0,2), (1,0)...
# Combinations and permutations
list(combinations('ABCD', 2)) # AB, AC, AD, BC, BD, CD
list(permutations('ABC', 2)) # AB, AC, BA, BC, CA, CB
# Running total
list(accumulate([1,2,3,4])) # [1, 3, 6, 10]
# Group consecutive items
for key, group in groupby('AAABBBCC'):
print(key, list(group)) # A [A,A,A], B [B,B,B], C [C,C]
# Take first N from generator (no list conversion)
first_10 = list(islice(infinite_generator(), 10))
Why
itertools operates lazily on iterators, using O(1) memory regardless of input size. Essential for processing large datasets.
Revisions (0)
No revisions yet.