This cheatsheet contains diagrams that explain how the iterables from the module itertools work in a visual way.


batched

islice

pairwise

chain

groupby

filterfalse

takewhile & dropwhile

compress

repeat

cycle

count

zip_longest

starmap

accumulate

By default, accumulate uses addition as its operation.
The magic in using functools.reduce and itertools.accumulate well is in realising the function that combines elements can be an arbitrary function of two arguments.
For example, you can imagine you're a painter and you're going to mix the colours in your palette:

product

When you want to compute the product of an iterable with itself two or more times, you can also use the argument repeat:

permutations

combinations

combinations_with_replacement



Every Monday, you'll get a Python deep dive that unpacks a topic with analogies, diagrams, and code examples so you can write clearer, faster, and more idiomatic code.