DEV Community

Discussion on: Five++ cool Python snippets (Part - 2)

Collapse
hanpari profile image
Pavel Morava

I would write the last one this way:


numbers = [1,2,3,4,5,6,7,8,9]

windowed = zip(numbers, numbers[1:], numbers[2:])

print(list(windowed))

# [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9)]

Enter fullscreen mode Exit fullscreen mode
Collapse
apoorvtyagi profile image
Apoorv Tyagi Author

Great, This is also a nice way to do that 👍