0

I've been having this problem (below).

What I want to achieve is to shift each individual element in the list to the right and create a list/dictionary of lists of the possible combinations.

This is what I would like to achieve:

{[1,2,3,4,5],
[2,1,3,4,5],
[2,3,1,4,5],
[2,3,4,1,5],
[2,3,4,5,1],
[1,3,2,4,5],
[1,3,4,2,5],
[1,3,4,5,2],
[1,2,4,3,5],
.
.
.
#until final possible combination
}

It doesn't have to be in the same order as the example above. I just want to be able to get a long list/dictionary of all possible order of combinations of [1,2,3,4,5]. Thanks in advance!

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

1

You can use permutations() from itertools:

from itertools import permutations
l = [1,2,3,4,5]
for i in permutations(l):
    print(list(l))

Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 5, 4]
[1, 2, 4, 3, 5]
[1, 2, 4, 5, 3]
[1, 2, 5, 3, 4]
[1, 2, 5, 4, 3]
[1, 3, 2, 4, 5]
[1, 3, 2, 5, 4]
[1, 3, 4, 2, 5]
[1, 3, 4, 5, 2]
[1, 3, 5, 2, 4]
[1, 3, 5, 4, 2]
[1, 4, 2, 3, 5]
[1, 4, 2, 5, 3]
[1, 4, 3, 2, 5]
[1, 4, 3, 5, 2]
[1, 4, 5, 2, 3]
[1, 4, 5, 3, 2]
[1, 5, 2, 3, 4]
[1, 5, 2, 4, 3]
[1, 5, 3, 2, 4]
[1, 5, 3, 4, 2]
[1, 5, 4, 2, 3]
[1, 5, 4, 3, 2]
[2, 1, 3, 4, 5]
[2, 1, 3, 5, 4]
[2, 1, 4, 3, 5]
[2, 1, 4, 5, 3]
[2, 1, 5, 3, 4]
[2, 1, 5, 4, 3]
[2, 3, 1, 4, 5]
[2, 3, 1, 5, 4]
[2, 3, 4, 1, 5]
[2, 3, 4, 5, 1]
[2, 3, 5, 1, 4]
[2, 3, 5, 4, 1]
[2, 4, 1, 3, 5]
[2, 4, 1, 5, 3]
[2, 4, 3, 1, 5]
[2, 4, 3, 5, 1]
[2, 4, 5, 1, 3]
[2, 4, 5, 3, 1]
[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