Skip to content

Latest commit

 

History

History
23 lines (16 loc) · 436 Bytes

20231216211604.md

File metadata and controls

23 lines (16 loc) · 436 Bytes

Easily iterate over pairs in a sequence

Before 3.10 you would use zip:

numbers = range(1, 6)
pairs = list(zip(numbers, numbers[1:]))
print(pairs)  # [(1, 2), (2, 3), (3, 4), (4, 5)]

Now you can use itertools' pairwise though:

from itertools import pairwise

numbers = range(1, 6)
pairs = list(pairwise(numbers))
print(pairs)  # [(1, 2), (2, 3), (3, 4), (4, 5)]

Great for sliding window problems! 🚀

#itertools