How to generate all possible permutations of a list with all possible lengths of N in Python

1 Answer

0 votes
import itertools

lst = [1, 2, 3]

permutations = []
for i in range(1, len(lst) + 1):
    permutations.extend(list(itertools.permutations(lst, r = i)))

print(permutations)





'''
run:

[(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

'''

 



answered Apr 20, 2021 by avibootz
...