Skip to main content
Image

r/Python


20 itertools functions, sorted by how often you'd actually reach for them (not the docs order)
20 itertools functions, sorted by how often you'd actually reach for them (not the docs order)
Resource

itertools is one of those modules everyone imports but most people only use 3-4 functions from. Went through all 20 and sorted them by how often you'd actually reach for each one in real code, not the order they show up in the docs.

Function Example What it does
chain() chain(it1, it2) Concatenates iterables, flattens multiple iterables into a single iterator.
chain.from_iterable() chain.from_iterable(nested) Flattens one level of nesting lazily, like chain(*nested) without unpacking.
islice() islice(it, start, stop) Slices an iterator, doesn't support negative indices.
compress() compress('ABCDEF', [1,0,1,0,1,1]) Filters data by a boolean selector iterable, yields A, C, E, F.
filterfalse() filterfalse(lambda x: x<5, [1,4,6,3,8]) Yields elements where the predicate is false, opposite of filter().
starmap() starmap(pow, [(2,5), (3,2)]) Applies a function with arguments unpacked from each tuple in the iterable.
pairwise() pairwise([1, 2, 3]) Returns overlapping pairs of consecutive elements: (1,2), (2,3) (3.10+).
batched() batched(range(10), 3) Splits an iterable into fixed-size tuples, last batch may be shorter (3.12+).
groupby() groupby(items, key=func) Groups consecutive elements by key, input must already be sorted by that key.
accumulate() accumulate(nums, func) Running totals or reductions, default is addition, accepts any binary function.
takewhile() takewhile(pred, it) Yields elements while the predicate is true, stops at the first false.
dropwhile() dropwhile(pred, it) Skips elements while the predicate is true, then yields everything remaining.
tee() tee(iterable, n=2) Splits one iterator into n independent iterators, use before the original is consumed.
zip_longest() zip_longest(a, b, fillvalue=None) Like zip() but continues to the longest input, filling shorter ones with fillvalue.
combinations() combinations(items, r) All r-length combinations, order doesn't matter, no repeated elements.
combinations_with_replacement() combinations_with_replacement('AB', 2) All r-length combinations allowing repeats, yields AA, AB, BB.
permutations() permutations(items, r) All r-length permutations, order matters, no repeated elements.
product() product(a, b) Cartesian product, equivalent to nested for-loops.
count() count(start=0, step=1) Infinite counter, useful with zip() or takewhile().
cycle() cycle([1, 2, 3]) Repeats an iterable infinitely, keeps a copy of all elements in memory.
repeat() repeat(10, times=3) Repeats an object n times, infinite if times is omitted.

pairwise and batched are the two most people don't know about, both fixed a "why am I hand-rolling this" moment for me. Curious which one on this list you use the least, or if I'm missing one people reach for a lot.


Advertisement: You know that idea you’ve always had? Codex makes it real. Try Codex for free today.
You know that idea you’ve always had? Codex makes it real. Try Codex for free today.
media poster



Friday Daily Thread: r/Python Meta and Free-Talk Fridays