Python

pairwise — Rolling Pair Iterator

admin by @admin ADMIN
Jun 17, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`pairwise([a, b, c, d])` yields `(a, b), (b, c), (c, d)` — built into itertools since 3.10. Perfect for "diff consecutive elements" patterns (deltas, rate-of-change, validation between rows).
Python
Raw
from itertools import pairwise

# Compute deltas between consecutive readings
readings = [10, 13, 12, 18, 22]
deltas = [b - a for a, b in pairwise(readings)]
print(deltas)                  # [3, -1, 6, 4]

# Validate that a list is strictly increasing
def is_increasing(xs):
    return all(a < b for a, b in pairwise(xs))

print(is_increasing([1, 2, 5, 10]))   # True
print(is_increasing([1, 2, 2, 5]))    # False
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.