Rodrigo Girão Serrão 🐍🚀 @mathspp.com · Jan 27

Before being added in Python 3.10, itertools.pairwise was commonly implemented in terms of itertools.tee. Isn't this elegant?

2 likes 1 replies

?

Replies

ax3man1ac · Jan 28

I'd say it's elegant... You can also scale it to be a sliding window of any size, something along the lines of: def window(iterable, n): its = [islice(it, offset, None) for offset, it in enumerate(tee(iterable, n))] yield from zip(*its) Then optionally: pairwise = partial(window, n=2)