How to pair every element with every other element in the row of a matrix with Python

1 Answer

0 votes
from itertools import combinations

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


pairs = [list(combinations(row, 2)) for row in matrix]
print(pairs)


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

 



answered Feb 10 by avibootz
...