typestar

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

  1. islice takes a slice of an endless count.
  2. chain and pairwise join and window sequences.
  3. accumulate and combinations cover running sums and pairs.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 5 in Generators & itertools, step 12 of 53 in Pythonic Python.

← Previous Next →