snippetpythonModeratepending
Python itertools recipes for common data tasks
Viewed 0 times
itertoolschunkedsliding windowflattengroupbyrecipes
Problem
Need efficient iterator-based solutions for grouping, chunking, sliding windows, and flattening.
Solution
Essential itertools recipes:
from itertools import islice, chain, groupby, zip_longest, accumulate
from collections import deque
# Chunk an iterable into fixed-size pieces
def chunked(iterable, n):
it = iter(iterable)
while chunk := list(islice(it, n)):
yield chunk
list(chunked(range(10), 3)) # [[0,1,2], [3,4,5], [6,7,8], [9]]
# Sliding window
def sliding_window(iterable, n):
it = iter(iterable)
window = deque(islice(it, n), maxlen=n)
if len(window) == n:
yield tuple(window)
for item in it:
window.append(item)
yield tuple(window)
list(sliding_window([1,2,3,4,5], 3)) # [(1,2,3), (2,3,4), (3,4,5)]
# Flatten nested iterables
def flatten(nested):
for item in nested:
if hasattr(item, '__iter__') and not isinstance(item, (str, bytes)):
yield from flatten(item)
else:
yield item
list(flatten([[1,[2,3]],[4,[5,[6]]]]) # [1,2,3,4,5,6]
# Group consecutive equal elements
data = [1,1,2,2,2,3,1,1]
for key, group in groupby(data):
print(key, list(group)) # 1 [1,1], 2 [2,2,2], 3 [3], 1 [1,1]
# Running total
list(accumulate([1,2,3,4,5])) # [1, 3, 6, 10, 15]
# Interleave iterables
def interleave(*iterables):
for values in zip_longest(*iterables):
yield from (v for v in values if v is not None)
list(interleave('abc', '12')) # ['a','1','b','2','c']
# First item matching condition (or default)
def first(iterable, predicate, default=None):
return next((x for x in iterable if predicate(x)), default)
first(range(100), lambda x: x > 50) # 51Why
itertools provides memory-efficient, lazy evaluation for data processing. These recipes avoid loading entire datasets into memory.
Context
Python data processing and transformation
Revisions (0)
No revisions yet.