How can I generate all possible permutations of a list in Python?

87 views

How can I generate all possible permutations of a list in Python?

How can I generate all possible permutations of a list in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Use the itertools.permutations() function.

import itertools

elements = [1, 2, 3]
permutations = itertools.permutations(elements)

for p in permutations:
    print(p)

Output:

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
solveurit24@gmail.com Changed status to publish February 13, 2025
0