how to use premutation itertool in python

itertools.permutation function is used to compute permutations of iterable. The following is the syntax to import permutations module from itertools:

main.py
from itertools import permutations
35 chars
2 lines

Once imported, you can use it to compute permutations as follows:

main.py
lst = [1, 2, 3]
permut = permutations(lst)
print(list(permut))
63 chars
4 lines

Output:

main.py
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
67 chars
2 lines

Here, permutations(lst) computes all the possible permutations of the elements in a list lst. Hope this helps!

related categories

gistlibby LogSnag