Skip to content

zip-instead-of-pairwise (RUF007)

Fix is sometimes available.

What it does

Checks for use of zip() to iterate over successive pairs of elements.

Why is this bad?

When iterating over successive pairs of elements, prefer itertools.pairwise() over zip().

itertools.pairwise() is more readable and conveys the intent of the code more clearly.

Example

letters = "ABCD"
zip(letters, letters[1:])  # ("A", "B"), ("B", "C"), ("C", "D")

Use instead:

from itertools import pairwise

letters = "ABCD"
pairwise(letters)  # ("A", "B"), ("B", "C"), ("C", "D")

Fix safety

The fix is always marked unsafe because it assumes that slicing an object (e.g., obj[1:]) produces a value with the same type and iteration behavior as the original object, which is not guaranteed for user-defined types that override __getitem__ without properly handling slices. Moreover, the fix could delete comments.

References