On this page
  1. itertools.pairwise
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

itertools.pairwise

Prev: itertools.islice | Up: itertools | Next: itertools.permutations
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.pairwise
Tags: #public #python3 #stdlib #itertools


Import

import itertools
from itertools import pairwise

Signature & Overview

itertools.pairwise(iterable, /)

Return an iterator of overlapping pairs taken from the input iterator.

Usage

from itertools import pairwise

assert list(pairwise([10, 20, 35, 50])) == [(10, 20), (20, 35), (35, 50)]
# Great for computing deltas
deltas = [b - a for a, b in pairwise([10, 20, 35, 50])]
assert deltas == [10, 15, 15]

Gotchas

REPL Inspect

from itertools import pairwise
help(pairwise)

Linked from

Related notes