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 in Python by Arvind
• 2,600 points
30 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']
answered Jul 11 by Neel
• 2,450 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 in Python by Arvind
• 2,600 points
25 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 ariaholic
• 7,320 points
70 views
0 votes
1 answer

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

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,710 points
64 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 in Python by Nymeria
• 3,520 points
41 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 169 views
+4 votes
6 answers
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 in Python by Neel
• 2,450 points
19 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 in Python by Neel
• 2,450 points
13 views