How do I generate permutations of length LEN given a list of N Items?

You should use the permutations function from the itertools module.

>>> import itertools
>>> lst = ['a','b','c','d','e','f','g','h','i','j']
>>> itertools.permutations(lst, 3)

Or, if you really want to get combinations, then use the combinations function.


In case you need all combinations of a list with length n where n may be larger than the list elements, and also with repeated elements:

import itertools
list(itertools.product([-1,1], repeat=3))

[(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]

imagine a cartesian product like [-1,1]x[-1,1]x[-1,1]


Assuming you're in python 2.6 or newer:

from itertools import permutations
for i in permutations(your_list, 3):
    print i

itertools.permutations(my_list, 3)