0

Apologies if I do not frame my question well, I'll try my best to do so:

How can I get a list to return every possible pair combination within it?

for instance

a = [1,2,3,4]

I would like to know how I can obtain a result like this:

a= [ [1,2], [1,3] , [1,4], [2,3] , [2,4] , [3,4] ]
czolbe
  • 571
  • 5
  • 18
  • Duplicate: https://stackoverflow.com/questions/2541401/pairwise-crossproduct-in-python – Alex Jun 27 '17 at 14:26

2 Answers2

1

You can make use of combinations in itertools modules!

>>> import itertools as it
>>> it.combinations([1,2,3,4],2)
<itertools.combinations object at 0x106260fc8>
>>> list(it.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
0
>>> import itertools
>>> a = [1,2,3,4]
>>> list(itertools.combinations(a, 2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Kevin
  • 74,910
  • 12
  • 133
  • 166