Possible Duplicate:
How to generate all permutations of a list in Python
I am given a list [1,2,3] and the task is to create all the possible permutations of this list.
Expected output:
[[1, 2, 3], [1, 3, 2], [2, 3, 1], [2, 1, 3], [3, 1, 2], [3, 2, 1]]
I can’t even think from where to begin. Can anyone help?
Thanks
itertools.permutations does this for you.
Otherwise, a simple method consist in finding the permutations recursively: you successively select the first element of the output, then ask your function to find all the permutations of the remaining elements.
A slightly different, but similar solution can be found at https://stackoverflow.com/a/104436/42973. It finds all the permutations of the remaining (non-first) elements, and then inserts the first element successively at all the possible locations.