The Wayback Machine - https://web.archive.org/web/20240804080132/https://www.geeksforgeeks.org/permutation-and-combination-in-python/
Open In App

Permutation and Combination in Python

Last Updated : 08 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.

Permutation 

First import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form. 
 

Python3




# A Python program to print all
# permutations using library function
from itertools import permutations
 
 
# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3])
 
# Print the obtained permutations
for i in list(perm):
    print (i)


Output: 

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

Time complexity: O(n!), where n is the length of the input list. This is because there are n! permutations of n elements, and the program generates and prints all of them.
Auxiliary space: O(n!), as the program needs to store all n! permutations in memory before printing them out. Specifically, the perm variable created by calling permutations([1, 2, 3]) stores all n! permutations in memory as a list.

It generates n! permutations if the length of the input sequence is n. 
If want  to get permutations of length L then implement it in this way. 

Python3




# A Python program to print all
# permutations of given length
from itertools import permutations
 
# Get all permutations of length 2
# and length 2
perm = permutations([1, 2, 3], 2)
 
# Print the obtained permutations
for i in list(perm):
    print (i)


Output: 

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

The time complexity of this program is O(n^r) where n is the length of the input array and r is the length of permutations to be generated. 

The space complexity is also O(n^r) as all permutations are stored in memory before printing.

It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.

Combination 

This method takes a list and an input r as an input and return an object list of tuples which contain all possible combination of length r in a list form. 
 

Python3




# A Python program to print all
# combinations of given length
from itertools import combinations
 
# Get all combinations of [1, 2, 3]
# and length 2
comb = combinations([1, 2, 3], 2)
 
# Print the obtained combinations
for i in list(comb):
    print (i)


Output: 

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

 

1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order. 
 

Python3




# A Python program to print all
# combinations of a given length
from itertools import combinations
 
# Get all combinations of [1, 2, 3]
# and length 2
comb = combinations([1, 2, 3], 2)
 
# Print the obtained combinations
for i in list(comb):
    print (i)


Output: 

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

 

2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination. 
 

Python3




# A Python program to print all combinations
# of given length with unsorted input.
from itertools import combinations
 
# Get all combinations of [2, 1, 3]
# and length 2
comb = combinations([2, 1, 3], 2)
 
# Print the obtained combinations
for i in list(comb):
    print (i)


Output: 

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

 

3. If we want to make a combination of the same element to the same element then we use combinations_with_replacement. 
 

Python3




# A Python program to print all combinations
# with an element-to-itself combination is
# also included
from itertools import combinations_with_replacement
 
# Get all combinations of [1, 2, 3] and length 2
comb = combinations_with_replacement([1, 2, 3], 2)
 
# Print the obtained combinations
for i in list(comb):
    print (i)


Output:

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

 



Similar Reads

Combination and Permutation Practice Questions | Set 1
Prerequisite : Permutation and Combination n students appear in an examination, find the number of ways the result of examination can be announced. Answer is 2n Examples: Input : n = 6 Output : Each student can either pass or fail in the examination. so ,there exists 2 possibilities for each of the 6 students in the result. hence total number of wa
3 min read
Check if permutation of one string can break permutation of another
Given two strings str1 and str2, the task is to check if any permutation of the given strings str1 and str2 is possible such that the character at each index of one string is greater than or equal to the other string. Examples: Input: A = "abc", B = "xya" Output: Yes Explanation: "ayx" is a permutation of B = "xya" which can break to string "abc" w
6 min read
Minimum number of given operations required to convert a permutation into an identity permutation
Given a permutation P (P1, P2, P3, ... Pn) of first n natural numbers. Find the minimum number of operations to convert it into an identity permutation i.e. 1, 2, 3, ..., n where each operation is defined as: P[i] = P[P[P[i]]] [Tex]\forall [/Tex]i from 1 to n (1 based indexing). If there is no way to convert then print -1.Examples: Input: arr[] = {
11 min read
Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
Given a permutation P of size N, having values from 1 to N. the task is to find the minimum number of adjacent swaps required such that for all i in the range [1, N], P[i] does not equal i.Examples: Input: P = [1, 4, 3, 5, 2] Output: 2 Explanation: Here P = [1, 4, 3, 5, 2] at index 1, 2, 3, 4, 5. As we can see, P[1] = 1 and P[3] = 3. Hence, we need
7 min read
Permutation transformation: Minimum operations to achieve permutation
Given an array arr[] of length n, the task is to find the minimum number of operations required to make a permutation of integers 1 to n where you are allowed to select any element arr[i] and replace it with arr[i] % x (x is any positive integer). If it is not possible to make a permutation return -1. Examples: Input: n = 5, arr[] = {2, 3, 4, 4, 5}
8 min read
Python | Print all string combination from given numbers
Given an integer N as input, the task is to print the all the string combination from it in lexicographical order. Examples: Input : 191 Output : aia sa Explanation: The Possible String digit are 1, 9 and 1 --> aia 19 and 1 --> sa Input : 1119 Output : aaai aas aki kai ks Approach: Get the String and find all its combination list in the given
2 min read
Python | Extract Combination Mapping in two lists
Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method : Using zip() + product() With these functions thi
2 min read
Count of factors of combination of N and K (nCk)
Given integers N and K, the task is to find the number of factors of NCK. Since the answer can be very large return the count of factors modulo 998244353. Example: Input: N = 5, K = 2Output: 4Explanation: 5C2 = 10 which have {1, 2, 5, 10} as its divisors. So answer would be 4. Input: N = 10, K = 3Output: 16 Approach: The problem can be solved based
11 min read
Largest number upto T formed by combination of As and Bs
Given numbers T, A, and B, the task is to find the greatest number less than equal to T formed by the sum of any combination of A and B (A and B can be used any number of times) along with one special operation that is at any instance sum can be halved (integer division by 2) by its current value at most one time. Examples: Input: T = 8, A = 5, B =
10 min read
Count number of strings (made of R, G and B) using given combination
We need to make a string of size n. Each character of the string is either ‘R’, ‘B’ or ‘G’. In the final string there needs to be at least r number of ‘R’, at least b number of ‘B’ and at least g number of ‘G’ (such that r + g + b <= n). We need to find number of such strings possible. Examples: Input : n = 4, r = 1, b = 1, g = 1. Output: 36 No.
7 min read
three90RightbarBannerImg