Show Menu
Cheatography

Python Itertools Cheat Sheet (DRAFT) by

Basic cheatsheet for python itertools.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Definition

The itertools module is a collection of tools intended to be fast and use memory effici­ently when handling iterators (like lists or dictio­nar­ies).

Example:

import itertools
From the Python 3 docume­ntation
The module standa­rdizes a core set of fast, memory efficient tools that are useful by themselves or in combin­ation. Together, they form an “iterator algebra” making it possible to construct specia­lized tools succinctly and effici­ently in pure Python.

Types of Iterators

Infinite Iterators

Iterator
Argument
Result
Example
count()
start, [step]
start, start+­step, start+­2*step, …
count(10) --> 10 11 12 13 14 ...
cycle()
p
p0, p1, … plast, p0, p1, …
cycle(­'ABCD') --> A B C D A B C D ...
repeat()
elem [,n]
elem, elem, elem, … endlessly or up to n times
repeat(10, 3) --> 10 10 10