-1

Let's say I have the following list lst=[1,1,2,2,3,3,4,4,5,5,6,6]. How can I use itertools to get all possible combinations of 4 numbers? My problem is that there are duplicates which I want to treat as the same subset for example [1,1,2,3] is the same as [1,1,2,3] even though the 1's are in different positions they represent the same set. Any idea how to proceed?

Example of what I tried: listOfCombinations = [x for x in itertools.combination(lst, 5)]

Cro-Magnon
  • 193
  • 7

1 Answers1

2

You can consider maintaining a set of your resulting combinations since a set doesn't allow duplicates:

lst = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]

res = set()
for c in itertools.combinations(lst, 4):
  res.add(c)

print(res)
# {(2, 5, 6, 6), (2, 4, 4, 6), (2, 4, 4, 5), (1, 4, 4, 5) ...}

Or, as @juanpa.arrivillaga mentioned, simply

res = set(itertools.combinations(lst, 4))
slider
  • 12,810
  • 1
  • 26
  • 42