Generating Unique Permutations in Python

python

Solution

`if i not in combos:` will take a long time because membership testing in a list is (worst-case) O(N) -- it has to scan through each element. You can use a `set` instead:

>>> from itertools import permutations
>>> x = ["$5", "$10", "$10", "TAX", "$5", "20%", "BOGO", "BOGO", "TAX", "BOGO"]
>>> %time p = set(permutations(x, 9))
CPU times: user 0.88 s, sys: 0.01 s, total: 0.90 s
Wall time: 0.90 s
>>> len(p)
75600

Problem

I am looking to find the unique permutations of the list, x = ["$5", "$10", "$10", "TAX", "$5", "20%", "BOGO", "BOGO", "TAX"] in groups of 9 What i'm currently doing is ``` from itertools import permutations x = ["$5", "$10", "$10", "TAX", "$5", "20%", "BOGO", "BOGO", "TAX"] combos = [] for i in permutations(x, 9): if i not in combos: combos.append(i) print combos ``` However, this takes far too long to run and I was wondering if someone could give me a more efficient solution.

Original source

Related problems