How to pair adjacent elements in each row of a matrix in Python

1 Answer

0 votes
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]

pairs = [list(zip(row[::2], row[1::2])) for row in matrix]
print(pairs)


   
'''
run:
 
[[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]]
   
'''

 



answered Feb 10 by avibootz
...