-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]] 
0

1 Answer 1

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:])) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.