Python program to print all permutations of a list

By IncludeHelp Last updated : February 17, 2024

Problem statement

Given a list, write a Python program to print all permutations of the given list.

Printing all permutations of a list

To print all permutations of a list, you can use the permutations() method after importing the permutations from the itertools module. The permutations() method returns successive permutations of elements in the iterable.

Syntax

Below is the syntax to get all permutations of a list in Python:

list_perm = permutations(list1)

Python code to print all permutations of a list

This code has a list (list1), we are getting & printing all permutation of list1.

# Import the module
from itertools import permutations

# Declaring a list
list1 = [10, 20, 30]

# Get the all permutations
list_perm = permutations(list1)

# Print the  permutations
for lst in list(list_perm):
    print(lst)

Output

The output of the above code is:

(10, 20, 30)
(10, 30, 20)
(20, 10, 30)
(20, 30, 10)
(30, 10, 20)
(30, 20, 10)

To understand the above code, you should have the basic knowledge of the following Python topics:

 
 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.