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

Download this cheatsheet

A4 itertools cheatsheet shown in light and dark themes.

A4 itertools cheatsheet shown in light and dark themes.

Download this cheatsheet


Reshaping

batched

Visual diagram explaining how batched works. Visual diagram explaining how batched works.

islice

Visual diagram explaining how islice works. Visual diagram explaining how islice works.

pairwise

Visual diagram explaining how pairwise works. Visual diagram explaining how pairwise works.

chain

Visual diagram explaining how chain works. Visual diagram explaining how chain works.

groupby

Visual diagram explaining how groupby works. Visual diagram explaining how groupby works.

Filtering

filterfalse

Visual diagram explaining how filterfalse works. Visual diagram explaining how filterfalse works.

takewhile & dropwhile

Visual diagram explaining how takewhile and dropwhile work. Visual diagram explaining how takewhile and dropwhile work.

compress

Visual diagram explaining how compress works. Visual diagram explaining how compress works.

Infinite

repeat

Visual diagram explaining how repeat works. Visual diagram explaining how repeat works.

cycle

Visual diagram explaining how cycle works. Visual diagram explaining how cycle works.

count

Visual diagram explaining how count works. Visual diagram explaining how count works.

Complementary

zip_longest

Visual diagram explaining how zip_longest works. Visual diagram explaining how zip_longest works.

starmap

Visual diagram explaining how starmap works. Visual diagram explaining how starmap works.

accumulate

Visual diagram explaining how accumulate works. Visual diagram explaining how accumulate works.

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:

Visual diagram explaining how accumulate works. Visual diagram explaining how accumulate works.

Combinatorial

product

Visual diagram explaining how product works. Visual diagram explaining how product works.

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

Visual diagram explaining how product works. Visual diagram explaining how product works.

permutations

Visual diagram explaining how permutations work. Visual diagram explaining how permutations work.

combinations

Visual diagram explaining how combinations work. Visual diagram explaining how combinations work.

combinations_with_replacement

Visual diagram explaining how combinations_with_replacement works. Visual diagram explaining how combinations_with_replacement works.


Download this cheatsheet

A4 itertools cheatsheet shown in light and dark themes.

A4 itertools cheatsheet shown in light and dark themes.

Download this cheatsheet

Become the smartest Python ๐Ÿ developer in the room ๐Ÿš€

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.

Previous Post Next Post