Permutation and Combination in Python
Last Updated :
09 May, 2025
Python provides built-in methods to work with permutations and combinations using the itertools module. These are helpful in problems involving arrangement (order matters) and selection (order doesn’t matter) of elements.
Let's explore them one by one:
Permutation
A permutation is an arrangement of elements where the order matters. For example, (1, 2) and (2, 1) are two different permutations.
Python offers the permutations() method in the itertools module to generate these.
Example 1: Get All Permutations of a List
Python
from itertools import permutations
perm = permutations([1, 2, 3])
for i in perm:
print(i)
Output(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
Explanation: permutations([1, 2, 3]) returns all possible orderings (3! = 6 permutations).
Example 2: Permutations of Specific Length
We can also generate permutations of a specific length r.
Python
from itertools import permutations
perm = permutations([1, 2, 3], 2)
for i in perm:
print(i)
Output(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
Explanation: permutations([1, 2, 3], 2) returns 2-length ordered arrangements (nPr = 6).
Combination
A combination is a way to select elements where the order does not matter. For example, (1, 2) and (2, 1) are considered the same combination.
Python offers the combinations() method for this purpose.
Example 1: Get All Combinations of Length 2
Python
from itertools import combinations
comb = combinations([1, 2, 3], 2)
for i in comb:
print(i)
Output(1, 2)
(1, 3)
(2, 3)
Explanation:
- combinations([1, 2, 3], 2) returns all 2-element subsets without regard to order.
- Duplicates are avoided (e.g., (2,1) is not separate from (1,2)).
Example 2: Combinations from an Unsorted List
Python
from itertools import combinations
comb = combinations([2, 1, 3], 2)
for i in comb:
print(i)
Output(2, 1)
(2, 3)
(1, 3)
Explanation:
- combinations([2, 1, 3], 2) preserves the input order in output combinations.
- Elements are treated uniquely based on their position in the list.
Example 5: Combinations with Repetition
Use combinations_with_replacement() to allow repeated elements in combinations.
Python
from itertools import combinations_with_replacement
comb = combinations_with_replacement([1, 2, 3], 2)
for i in comb:
print(i)
Output(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
Explanation: combinations_with_replacement([1, 2, 3], 2) includes combinations like (1, 1), allowing repetition.
Related articles:
Similar Reads
Combinatoric Iterators in Python An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the 'yield' function. Generators can be recursive jus
4 min read
Combinatoric Iterators in Python An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the 'yield' function. Generators can be recursive jus
4 min read
Combinatoric Iterators in Python An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the 'yield' function. Generators can be recursive jus
4 min read
SymPy | Permutation.min() in Python Permutation.min() : min() is a sympy Python library function that returns the minimum value in the permutation. Syntax : sympy.combinatorics.permutations.Permutation.min() Return : minimum value in the permutation Code #1 : min() Example Python3 1=1 # Python code explaining # SymPy.Permutation.min()
1 min read
Itertools Combinations() function - Python The combinations() function in Python, part of the itertools module, is used to generate all possible combinations of a specified length from a given iterable (like a list, string, or tuple). Unlike permutations, where the order does matter, combinations focus only on the selection of elements, mean
2 min read
Python | SymPy Permutation.index() method Permutation.index() : index() is a sympy Python library function that returns the index value of the permutation in argument. Index of a permutation = Sum of all subscripts j such that permutation[j] is greater than permutation[j+1]. Syntax : sympy.combinatorics.permutations.Permutation.index() Retu
1 min read