Suppose I have a list of integers:
myList = [1,2,3,4]
How would one return every possible combination of these two subsets with Python.
For example, set [1,2,3,4] would produce [1,2], [3,4] and [1,3], [2,4] and [1,4],[2,3]
Suppose I have a list of integers:
myList = [1,2,3,4]
How would one return every possible combination of these two subsets with Python.
For example, set [1,2,3,4] would produce [1,2], [3,4] and [1,3], [2,4] and [1,4],[2,3]
itertools
can give you all combinations of a list of size you define (here n
):
import itertools
myList = [1,2,3,4]
n=3
for subset in itertools.combinations(myList , n):
print(subset)
output:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)