Table of contents
1.
Introduction
2.
What is Permutation in Python?
3.
What is Combination in Python?
4.
Permutation and Combination in Python
5.
Difference Between Permutation and Combination
6.
Example of Permutation and Combination in Python
7.
Frequently Asked Questions
7.1.
How to do Permutation and Combination in Python?
7.2.
Is permutation and combination important for coding?
7.3.
What is the use of permutation in Python? 
7.4.
How do you write a combination formula in Python?
8.
Conclusion
Last Updated: Jan 7, 2025
Easy

Permutation and Combination in Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will explore Permutation and Combination in Python, which are fundamental concepts in mathematics and programming. Permutations deal with arranging items in specific orders, while combinations focus on selecting items without considering the order. We'll discuss how to implement these concepts using Python's built-in libraries, providing examples to enhance understanding and practical usage.

Permutation and combination in Python

The article explains the details of Permutation and Combination in Python. Let's get started.

Read more, Fibonacci Series in Python, and Convert String to List Python

What is Permutation in Python?

A permutation is an arrangement of a set where the order is important. Permutation() is a built-in method of the Python itertools module that finds permutations. Let's analyse the illustration.

Also read, Permutation of string

Example - 1

from itertools import permutations  
permu = permutations(['1','2','3'])  
print(permu)  
for p in list(permu):  
   print(p) 
You can also try this code with Online Python Compiler
Run Code


Output 

We have imported the itertools module into the code above. We used the itertools object returned by the permutation() method, which accepts a string as an input. To obtain each permutation, a for loop must be used.

('1', '2', '3')
('1', '3', '2')
('2', '1', '3')
('2', '3', '1')
('3', '1', '2')
('3', '2', '1')

Example - 2

from itertools import permutations  
permu = permutations(['4','5'])  
print(permu)  
for p in list(permu):  
   print(p)
You can also try this code with Online Python Compiler
Run Code


Output 

('4', '5')
('5', '4')

Example - 3

from itertools import permutations  
permu = permutations(['C','N'])  
print(permu)  
for p in list(permu):  
   print(p)
You can also try this code with Online Python Compiler
Run Code


Output 

('C', 'N')
('N', 'C')

Permutation of the fixed length

When only a specific number of combinations of each element are used, we can calculate the permutation of the fixed length set. Let's analyse the example.

Example 

from itertools import permutations  
permu = permutations(['n','i','n','j','a'],2)  
print(permu)  
for p in list(permu):  
   print(p)
You can also try this code with Online Python Compiler
Run Code

 

Output 

('n', 'i')
('n', 'n')
('n', 'j')
('n', 'a')
('i', 'n')
('i', 'i')
('i', 'j')
('i', 'a')
('n', 'i')
('n', 'n')
('n', 'j')
('n', 'a')
('j', 'i')
('j', 'n')
('j', 'j')
('j', 'a')
('a', 'i')
('a', 'n')
('a', 'j')


You can also read about the Multilevel Inheritance in Python, Swapcase in Python

What is Combination in Python?

A Combination is a grouping of elements in which the sequence is irrelevant. The combination() function in the Python itertools module allows users to compute the combination of input data. 

We can determine a string's combination. Let's analyse the following example.

Example - 1

from itertools import combinations
combi = combinations([1, 2, 3], 2)
for i in list(combi):
print (i)
You can also try this code with Online Python Compiler
Run Code

 

Output

(1, 2)
(1,3)
(2,3)

 

 Example - 2

from itertools import combinations
combi = combinations(['n', 'i', 'n', 'j', 'a'], 2)
for i in list(combi):
print (i)
You can also try this code with Online Python Compiler
Run Code

 

Output

('n', 'i')
('n', 'n')
('n', 'j')
('n', 'a')
('i', 'n')
('i', 'j')
('i', 'a')
('n', 'j')
('n', 'a')
('j', 'a')

Use combinations with replacement if you want to combine the same elements with same elements.
 

Example 

from itertools import combinations_with_replacement
combi = combinations_with_replacement([1, 2, 4], 2)
for i in list(combi):
print (i)
You can also try this code with Online Python Compiler
Run Code


Output

(1, 2)
(1,2)
(1,4)
(2,2)
(2,4)
(4,4)

Permutation and Combination in Python

Permutation and Combination in Python play a crucial role in mathematics. The itertools library, which comes with built-in functions to compute Permutation and Combination in Python.

We must import the itertools library to calculate the Permutation and Combination in Python. Using the command below, we can import it.

import itertools 
You can also try this code with Online Python Compiler
Run Code

Difference Between Permutation and Combination

In this section of the article, we will see the basic differences between permutation and combination. 

PermutationCombination
If we try to arrange a given number of objects considering their order in the arrangement, then these arrangements are called permutations. If we select a few objects from a given number of objects, these selections are called combinations. 
The basic formulae to calculate the total number of permuatations formed by taking r objects at a time from a number of distinct objects are - n!/(n-r)!, n>=rThe basic formulae to calculate the total number of combinations formed by taking r objects at a time from a number of distinct objects are - n!/(r!)(n-r)!, n>=r
We have three numbers 1,2,3, now lets see how many total permutations are {1,2},{1,3}, {2,1}, {3,1}, {3,2}, {2,3}We have three numbers 1,2,3, now lets see how many total combinations are{1,2},{1,3}, {3,2}
Permutation() is a built-in method() of the python itertools module that finds permutations. Combinations is a built-in method() of the python itertools module that finds Combinations.  
Permutations are used in scenarios where the order or sequence is important. Examples include ranking positions (1st, 2nd, 3rd) in a race, password creation, or arranging books on a shelf.Combinations are used when the order is irrelevant. Examples include selecting a committee, picking lottery numbers, or choosing a group of items without caring about the order.
Permutations generate more results than combinations because the different orders of the same items count as separate permutations.Combinations generate fewer results since only the selection of items matters, not their order.

Example of Permutation and Combination in Python

 Example of permutation.

from itertools import permutations  
permu = permutations(['1','2','3'],2)  
print(permu)  
for p in list(permu):  
   print(p) 
You can also try this code with Online Python Compiler
Run Code

 

Output 

('1', '2')
('1','3')
('2', '1')
('2', '3')
('3','1')
('3','2')

 

Example of combination.

from itertools import combinations
combi = combinations([1, 2, 3], 2)
for i in list(combi):
print (i)
You can also try this code with Online Python Compiler
Run Code

Output

(1,2)
(1,3)
(2,3)

Frequently Asked Questions

How to do Permutation and Combination in Python?

For the Permutation and Combination in Python, first import the itertools package. The output of this method, which accepts a list as its input, is an object list of tuples that contains all possible permutations in list form.

Is permutation and combination important for coding?

Permutations and combinations are crucial in coding for solving problems related to arrangements and selections, such as generating possible outcomes, optimizing algorithms, and analyzing data. They enhance problem-solving skills in areas like game development, cryptography, and statistical modeling.

What is the use of permutation in Python? 

In Python, the itertools.permutations() function generates all possible arrangements of a given iterable. This is useful for tasks like generating test cases, solving puzzles, or exploring different configurations of data, enabling developers to handle complex scenarios efficiently.

How do you write a combination formula in Python?

The total number of combinations (the number of various ways to do this) if we choose k items from a total of n alternatives and don't care about their order is: C(n,k)=(nk)=n!k! (nk)!

Conclusion

In this article, we discussed permutations and combinations in Python, explaining what they are and why they are important. We also looked at different ways to use permutations and combinations in Python. 

Recommended Reads: Permutation of string

You can refer to our guided paths on the Code360 You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Live masterclass