How to generate all permutations of a list in Python?


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)]

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 05-Mar-2020

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements