0

I want to pick the pair of 3 elements from list and so on . How can I do this ?

Example:

array = [10, 5, 2, 7, 8, 7] 

Output:

[[10, 5, 2], [7, 8, 7]]

My code:

array = [10, 5, 2, 7, 8, 7] 
new_list = [array[i:i+3] for i in range(len(array))]
Hamza
  • 391
  • 2
  • 16
  • 1
    Almost correct; you want `for i in range(0, len(array), 3)` instead, so that it jumps 3 by 3 and not 1 by 1. – Stef Sep 11 '21 at 10:06

0 Answers0