How do you generate all the permutations of a list in Python, independently of the type of elements in that list.
For example:
permutations ([])
[]
permutations ([1,])
[1]
permutations ([1,2])
[1, 2]
[2, 1]
permutations ([1,2,3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
EDIT: Eliben pointed to a solution that's similar to mine altough simpler, so I'm choosing it as the Accepted Answer, altough apparently Python 2.6 (that hasn't been released yet) will have a builtin solution in the itertools module:
import itertools
itertools.permutations([1,2,3])