2

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?

Shaun Han
  • 2,676
  • 2
  • 9
  • 29

6 Answers6

3

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]]
user2390182
  • 72,016
  • 6
  • 67
  • 89
3

Python 3.10 now has pairwise(). The items will be tuples.

>>> list(itertools.pairwise(range(10)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
2

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]]
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
2

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]]
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • @AlainT. My comment was directed at a previous version that used `zip(lst[:-1],lst[1:])`. Now this is common and fine. – user2390182 Sep 22 '21 at 17:40
1

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]]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
1

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]]
gold_cy
  • 13,648
  • 3
  • 23
  • 45