Say I have a list from 0 to 9:
lst = list(range(10))
I want to split it into individual steps of 2. I managed to write the following working code:
res = [[] for _ in range(len(lst) - 1)]
for i, x in enumerate(lst):
if i < len(lst) - 1:
res[i].append(x)
if i > 0:
res[i-1].append(x)
>>> print(res)
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
But I feel like there should be more elegant way to code this. Any suggestions?