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

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


itertools.permutations(my_list, 3)


Assuming you're in python 2.6 or newer:

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


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 itertoolslist(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]