I know it's late, but I want to add an item.
set(itertools.combinations(t, 4)) excellent set(itertools.combinations(t, 4)) for most cases, but still internally repeats all repeated combinations, so it can be computationally difficult. This is especially true if there are not many actual unique combinations.
This repeats only unique combinations:
from itertools import chain,repeat,islice,count from collections import Counter def combinations_without_repetition(r, iterable=None, values=None, counts=None): if iterable: values, counts = zip(*Counter(iterable).items()) f = lambda i,c: chain.from_iterable(map(repeat, i, c)) n = len(counts) indices = list(islice(f(count(),counts), r)) if len(indices) < r: return while True: print(indices) yield tuple(values[i] for i in indices) for i,j in zip(reversed(range(r)), f(reversed(range(n)), reversed(counts))): if indices[i] != j: break else: return j = indices[i]+1 for i,j in zip(range(i,r), f(count(j), islice(counts, j, None))): indices[i] = j
Using:
>>> t = [2,2,2,2,4] # elements in t must be hashable >>> list(combinations_without_repetition(4, iterable=t)) [(2, 2, 2, 2), (2, 2, 2, 4)] # You can pass values and counts separately. For this usage, values don't need to be hashable # Say you have ['a','b','b','c','c','c'], then since there is 1 of 'a', 2 of 'b', and 3 of 'c', you can do as follows: >>> list(combinations_without_repetition(3, values=['a','b','c'], counts=[1,2,3])) [('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'c'), ('b', 'b', 'c'), ('b', 'c', 'c'), ('c', 'c', 'c')] # combinations_without_repetition() is a generator (and thus an iterator) # so you can iterate it >>> for comb in combinations_without_repetition(4, t): ... print(sum(comb)) ... 8 # 2+2+2+2 10 # 2+2+2+4
Please note that itertools.combinations() implemented in C, which means that in most cases it works much faster than my python script. This code works better than the set(itertools.combinations()) method only when there are set(itertools.combinations()) MORE duplicate combinations than unique combinations.