The itertools toolkit in Python
Standard-library building blocks for iterators.
import itertools
first_ten = list(itertools.islice(itertools.count(1), 10))
merged = list(itertools.chain("abc", "def"))
pairs = list(itertools.pairwise([1, 3, 6, 10]))
repeated = list(itertools.repeat("na", 4))
running = list(itertools.accumulate([1, 2, 3, 4]))
decks = itertools.product("AB", [1, 2])
combos = list(itertools.combinations("abc", 2))
How it works
islicetakes a slice of an endlesscount.chainandpairwisejoin and window sequences.accumulateandcombinationscover running sums and pairs.
Keywords and builtins used here
list
The run, in numbers
- Lines
- 11
- Characters to type
- 353
- Tokens
- 121
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 69 seconds.
Step 3 of 5 in Generators & itertools, step 12 of 53 in Pythonic Python.