Permutation and Combination in Python

11 Apr, 2022
Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.
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 functionfrom itertools import permutations # Get all permutations of [1, 2, 3]perm = permutations([1, 2, 3]) # Print the obtained permutationsfor 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)
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 lengthfrom itertools import permutations # Get all permutations of length 2# and length 2perm = permutations([1, 2, 3], 2) # Print the obtained permutationsfor i in list(perm):    print (i)
Output: 
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.
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 lengthfrom itertools import combinations # Get all combinations of [1, 2, 3]# and length 2comb = combinations([1, 2, 3], 2) # Print the obtained combinationsfor 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 lengthfrom itertools import combinations # Get all combinations of [1, 2, 3]# and length 2comb = combinations([1, 2, 3], 2) # Print the obtained combinationsfor 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 2comb = combinations([2, 1, 3], 2) # Print the obtained combinationsfor 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 includedfrom itertools import combinations_with_replacement # Get all combinations of [1, 2, 3] and length 2comb = combinations_with_replacement([1, 2, 3], 2) # Print the obtained combinationsfor i in list(comb):    print (i)
Output:
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3) 
 
sandeepkumarkasera1
simmytarika5
permutation
Combinatorial
Python
permutation
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subsets with sum equal to X
Find the K-th Permutation Sequence of first N natural numbers
Find the Number of Permutations that satisfy the given condition in an array
Count Derangements (Permutation such that no element appears in its original position)
Stack Permutations (Check if an array is stack permutation of other)
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe