I came across a ranking of itertools functions by how often they get used. Which ones are most useful in your kind of Python work, and which gotchas are worth knowing?
Which itertools functions do you actually reach for most?
Answers
There probably isn't a useful universal ranking here. For data processing and experiments, product can be a natural fit for generating combinations of parameters. Batched is handy when you need to process items in chunks, while cycle and repeat have their own uses.
Batched was added in Python 3.12, so check your supported versions before relying on it. The final batch can be shorter than the requested size.
Reply to this answer
Chain and chain.from_iterable are good ones to learn early. The latter flattens one level of nested iterables lazily, without first unpacking all the inner iterables. That distinction matters when the outer iterable is large or itself lazy.
Islice is useful for taking a portion of an iterator without converting the whole thing to a list. Just don't expect negative indices to work.
Reply to this answer
Groupby has a gotcha that catches people out. It groups consecutive items with the same key, not every matching item across the whole input. If you want one group per key, sort by that key first, unless the input is already arranged appropriately.
Also, the group iterators share the underlying input. Consume or save a group before advancing the outer iterator if you need its contents later.
Reply to this answer
For argument tuples, starmap is a neat alternative to writing out the unpacking in a comprehension. Compress is useful when you already have a separate iterable of boolean selectors. Filterfalse is closer to the opposite of filter, where the predicate decides what to exclude.
Reply to this answer
Don't overlook the difference between zip and zip_longest. Ordinary zip stops at the shortest input. If equal lengths are required, zip with strict=True raises an error when it detects a mismatch. If unequal lengths are expected and you want the remaining values, zip_longest fills the gaps instead.
g805+bradawl_2:
That distinction helps. Strict checking and filling missing values solve pretty different problems.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Loxby-3n:
And product can generate a lot of results quickly as you add inputs, even though it returns an iterator.