Skip to content Skip to sidebar Skip to footer

How To Find All Combinations Of Two Numbers In A List?

Let's say I have a list of four values. I want to find all combinations of two of the values. For example, I would like to get an output like: ((0, 1), (0, 2), (0, 3), (1, 2), (1,

Solution 1:

It is very easy

from itertools import combinations
list(combinations([0,1,2,3],2))

Solution 2:

Take just the lower triangular matrix if you only need a distinct set

a = [1,2,10,20]
[(a[i], a[j+i+1]) for i in range(len(a))  for j in range(len(a[i+1:]))]

[(1, 2), (1, 10), (1, 20), (2, 10), (2, 20), (10, 20)]

Post a Comment for "How To Find All Combinations Of Two Numbers In A List?"