How to get permutations of list or a set in Python

0 votes
I am new to Python. I want a program that will print all the permutations of a set or list of numbers.How to do it?py
Jul 11, 2019 in Python by Arvind
• 3,040 points
8,549 views

1 answer to this question.

0 votes

Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by  n!.  For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}.

# Python function to print permutations of a given list

def permutation(lst):

    # If lst is empty then there are no permutations

    if len(lst) == 0:

        return []

    # If there is only one element in lst then, only

    # one permuatation is possible

    if len(lst) == 1:

        return [lst]

  
    # Find the permutations for lst if there are

    # more than 1 characters

    l = [] # empty list that will store current permutation

 
    # Iterate the input(lst) and calculate the permutation

    for i in range(len(lst)):

       m = lst[i]

       # Extract lst[i] or m from the list.  remLst is

       # remaining list

       remLst = lst[:i] + lst[i+1:]

       # Generating all permutations where m is first

       # element

       for p in permutation(remLst):

           l.append([m] + p)

    return l

# Driver program to test above function

data = list('123')

for p in permutation(data):

    print p
Output:
['1', '2', '3']
['1', '3', '2']
['2', '1', '3']
['2', '3', '1']
['3', '1', '2']
['3', '2', '1']


Hope this Helps!!

To learn more, join the online course to do Masters in Python.

Thanks!

answered Jul 11, 2019 by Neel
• 3,020 points

Related Questions In Python

0 votes
1 answer

How to get a list of built in modules in Python?

simply open the python shell and type ...READ MORE

answered Aug 1, 2019 in Python by Arvind
• 3,040 points
998 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
1,053 views
0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,165 views
0 votes
1 answer

How to zip with a list output in Python instead of a tuple output?

Good question - Considering that you are ...READ MORE

answered Feb 7, 2019 in Python by Nymeria
• 3,560 points
1,374 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,007 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,387 views
0 votes
1 answer

How to add elements to a list in Python?

To initialize an empty list do this: new_list ...READ MORE

answered Jul 23, 2019 in Python by Neel
• 3,020 points
483 views
+1 vote
1 answer

How to print a pyramid of asterisks in Python?

This is one of the most frequently ...READ MORE

answered Aug 1, 2019 in Python by Neel
• 3,020 points
3,852 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP