I have a list of even N elements and would like to get a list of lists of all possible and unique pairs, e.g.:
list = [1, 2, 3, 4, 5, 6]
result: [[(1,2), (3,4), (5,6)], [(1,2), (3,5), (4,6)] ...
So each list I get as the result should have N/2 elements (pairs with unique numbers). This question seemed similar to my problem, although the answer gives the lists with 2 combinations only and it doesn't work for N > 4; not sure if it's possible to rework this solution for my purposes.
I suppose that one possible option is to:
- iterate through each possible order of N numbers (123456, 123465 ... 654321)
- create a list of pairs for each following 2 elements ([1,2,3,4,5,6] -> [(12), (34), (56)])
- sort those pairs and eliminate duplicates
But it feels that there should be a more elegant solution, would be grateful for help :)