Skip to content

How To Find Permutation And Combination Using Python?

In this article, you are going to learn how to find permutation and combination using Python programming language.

Well, Python has a huge library and packages that enable you to perform any task whether it is a mathematical or another task such as sending mail, performing google search etc.

Finding permutations and combinations of a given sequence also involves the use of a python package called itertools. It involves very easy steps which are described below, you can take our Python training program for deep understanding of Permutation and Combination in python.

Steps To Find Permutation And Combination

Step1: Importing itertools package

To implement permutation and combination, you will have to first import itertools package.

Step2: Getting all permutations/combinations

This is done by entering a list of items(defining sequence of items) as an input that will return all permutations and combinations in the form of list of tuples.

You can also define a particular length of permutations and combinations.

Step3: Printing the result

Generally, for loop is used to print all the permutations and combinations.

Finding Permutations Using Python

We have shown below two examples to find out all the permutations related to a sequence of alphabets.

Example 1: Without determining the length of the permutation

# Importing itertools package
from itertools import permutations
#Getting all permutations
per = permutations([‘a’,’b’,’c’])
#Printing the result
for p in list(per):  
 print p

Output 1: The following output will be generated after running the above codes.

(‘a’, ‘b’, ‘c’)
(‘a’, ‘c’, ‘b’)
(‘b’, ‘a’, ‘c’)
(‘b’, ‘c’, ‘a’)
(‘c’, ‘a’, ‘b’)
(‘c’, ‘b’, ‘a’)

Example2: By determining the length of the permutation.

# Importing itertools package
from itertools import permutations
#Getting all permutations and defining the length of the permutation
per = permutations([‘a’,’b’,’c’],2)
#Printing the result
for p in list(per): 
  print p

Output 2: The following output will be generated after running the above codes.

(‘a’, ‘b’)
(‘a’, ‘c’)
(‘b’, ‘a’)
(‘b’, ‘c’)
(‘c’, ‘a’)
(‘c’, ‘b’)

Finding Combinations Using Python

We have shown below two examples to find out all the combinations of a given sequence.

Example 1: Determining the length of the combination

# Importing itertools package
from itertools import combinations
#Getting all combinations by defining a particular length
com = combinations([‘p’,’q’,’r’,’s’],3)
#Printing the result
for c in list(com):
   print c

Output 1: You will get the following output.

(‘p’, ‘q’, ‘r’)
(‘p’, ‘q’, ‘s’)
(‘p’, ‘r’, ‘s’)
(‘q’, ‘r’, ‘s’)

Example2: Combinations follows lexical order which means the output will correspond to the sorted input.

# Importing itertools packagefrom itertools import combinations#Getting all combinations by defining a particular lengthcom = combinations([‘r’, ‘p’, ‘q’,], 2)#Printing the resultfor c in list(com):   print c

Output 2: You will get the following output.

(‘r’, ‘p’)(‘r’, ‘q’)(‘p’, ‘q’)

Example3: Combinations_with_replacements is used when you want to print the combination of same elements.

# Importing itertools package
from itertools import combinations_with_replacement
#Getting all combinations by defining a particular length
com = combinations_with_replacement([‘a’, ‘b’, ‘c’], 2)
#Printing the result
for c in list(com):   
print c

Output 3: You will get the following output.

(‘a’, ‘a’)
(‘a’, ‘b’)
(‘a’, ‘c’)
(‘b’, ‘b’)
(‘b’, ‘c’)
(‘c’, ‘c’)
Facebook
Twitter
LinkedIn
Pinterest

Online Digital Marketing Course with 5 Days Free Classes.

Are you one of them who think Online classes are not practical and Interactive.

Start with 5 Days Free Classes, to experience our quality of training Before Enrollment.