-3

Let's suppose we make this list

a=[1,2,3]

a can be sorted in 6 possible ways

a=[1,2,3]
a=[1,3,2]
a=[2,1,3]
a=[2,3,1]
a=[3,1,2]
a=[3,2,1]

How do I make a function that automatically does that but with bigger lists?

1 Answers1

2

The thing is called permutations and you can use itertools lib.

Here is the example:

from itertools import permutations

print list(permutations([1, 2, 3]))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
vovaminiof
  • 511
  • 1
  • 4
  • 13