-1

The command permutation from itertools

permutations([0,1,1]) 

returns

(0, 1, 1), (0, 1, 1), (1, 0, 1), (1, 1, 0), (1, 0, 1), (1, 1, 0)

is there a way to return

(0,1,1), (1,0,1), (1,1,0)

that is, for an arbitrary list of integers get all permutations, but without repeated elements if elements in the original list repeat?

Jake B.
  • 435
  • 3
  • 13

1 Answers1

0

You can cast the returned value to a set:

print(list(set(permutations([0,1,1]))))

Output:

[(0, 1, 1), (1, 1, 0), (1, 0, 1)]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • 2
    This is certainly a _naive_ way of doing it. – cs95 Dec 30 '17 at 18:48
  • 2
    The problem is that the number of distinguishable permutations can be *much* smaller than the total number of permutations, so this is in general extremely inefficient. It all depends on the problem size of course. For some use cases it would be pointless to do anything more, so it probably merits a +1 – John Coleman Dec 30 '17 at 18:51