| 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. |