How to generate all permutations of a list in Python
Question: 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 although simpler, so
I'm choosing it as the accepted answer, although Python 2.6+ has a builtin
solution in the **itertools** module:

    
    
    import itertools
    itertools.permutations([1, 2, 3])
    


Answer: And in [Python 2.6](http://docs.python.org/dev/whatsnew/2.6.html) onwards:

    
    
    import itertools
    itertools.permutations([1,2,3])
    

(returned as a generator. Use `list(permutations(l))` to return as a list.)

