0

I have a Python list with a variable number of elements.

[a,b,c,d,e]

I need to rearrange these elements inside the list and find all possible unique combinations.

[a,b,c,d,e]
[d,b,a,e,c]
[e,d,a,c,b], etc.

I cannot have same elements repeated, or have any elements removed:

[a,a,a,d,e]  <-- bad
mcu
  • 3,002
  • 7
  • 36
  • 59
  • 1
    OK. Now, what have you tried to solve this, and can you show your code and explain what difficulties you are facing in your implementation? – idjaw Jul 22 '17 at 23:50

2 Answers2

2

You should use itertools.permutations, for example:

import itertools
my_list = [a, b, c, d]
for x in itertools.permutations(my_list):
    print(x)
Mohd
  • 5,370
  • 7
  • 18
  • 29
1

You want itertools.permutations([a,b,c,d,e]).

Alex Hall
  • 33,530
  • 5
  • 49
  • 82