0

I would like to reorder a python list:

a = [1,2,3,4,5,6,7,...]

to the following form:

[[1,2,3],[2,3,4],[3,4,5],...]

What is the fastest way to do that?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Torben
  • 335
  • 3
  • 17

1 Answers1

1

you can try:

>>> a = [1,2,3,4,5,6,7]
>>> new_list = []
>>> for index in range(len(a)-2):
    new_list.append(a[index:index+3])


>>> new_list
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61