How to generate all permutations of a list in Python?

PythonProgramming

You can use the itertools package's permutations method to find all permutations of a list in Python. You can use it as follows −

Example

import itertools
perms = list(itertools.permutations([1, 2, 3]))
print(perms)

Output

This will give the output −

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
raja
Published on 20-Apr-2018 10:50:25
Advertisements