Home > Net >  Most elegant way to split a sequence into individual steps in Python
Most elegant way to split a sequence into individual steps in Python

Time:09-23

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?

CodePudding user response:

You can use zip function along with list comprehension.

>>> lst = list(range(10))
>>> [list(elm) for elm in zip(lst, lst[1:])]
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]

CodePudding user response:

You can generalize to n steps:

def steps(lst, n=2):
    return [[*x] for x in zip(*(lst[i:] for i in range(n)))]

steps(range(10), 2)
# [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
steps(range(10), 3)
# [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]]

CodePudding user response:

For a list of tuples:

>>> list(zip(lst,lst[1:]))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]

For a list of lists:

>>> list(map(list, zip(lst,lst[1:])))
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]

CodePudding user response:

Here is a solution ​without zip, just using indexes, range, len and list comprehension:

lst = list(range(10))
res = [ [lst[i], lst[i 1]] for i in range(len(lst) - 1)]
print(res)
# [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]

CodePudding user response:

You can avoid zip completely and just use list comprehension to create the sliding window.

[lst[i: i   2] for i in range(0, len(lst) - 1)]
>> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
  • Related