How to pair elements with their index in the row of a matrix with Python

1 Answer

0 votes
matrix = [
    [14, 67, 98],
    [74, 35, 83],
    [90, 96, 88]
]

pairs = [[(i, val) for i, val in enumerate(row)] for row in matrix]
print(pairs)


   
'''
run:
 
[[(0, 14), (1, 67), (2, 98)], [(0, 74), (1, 35), (2, 83)], [(0, 90), (1, 96), (2, 88)]]
   
'''

 



answered Feb 10 by avibootz

Related questions

...