-1

I have a list of values, want to extract the first 3 values every time, and need to remove 1st index position value after iteration.

a = [11,12,13,14,15,16,17,18,19]

Output:

[[11,12,13],[12,13,14],[13,14,15],[14,15,16],[15,16,17],[16,17,18],[17,18,19]]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58

1 Answers1

0

A list comprehension is probably fairly straightforward to understand-

a = [11,12,13,14,15,16,17,18,19]
a_out = [a[i:i+3] for i in range(len(a)-2)]

Or you can cut and piece together portions of the list with zip too, if this approach makes more sense -

a_out = list(zip(a[0:-2], a[1:-1], a[2:]))
Mortz
  • 4,654
  • 1
  • 19
  • 35