Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Permutation and Combination in Python?
In this section, we are going to learn how to find permutation and combination of a given sequence using Python programming language.
Python's itertools module provides built-in functions to generate permutations and combinations efficiently. A permutation is an arrangement of items where order matters, while a combination is a selection where order doesn't matter.
Understanding Permutations vs Combinations
Before diving into code, let's understand the difference:
- Permutation: Order matters. For items [A, B], permutations are (A,B) and (B,A)
- Combination: Order doesn't matter. For items [A, B], there's only one combination: (A,B)
Working with Permutations
Basic Permutations
Let's find all permutations of a list using itertools.permutations() ?
from itertools import permutations
items = ['a', 'b', 'c']
seq = permutations(items)
for p in seq:
print(p)
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
Permutations with Specific Length
You can specify the length of permutations using the second parameter ?
from itertools import permutations
letters = ['p', 'y', 't', 'h', 'o', 'n']
seq = permutations(letters, 2)
for p in seq:
print(p)
('p', 'y')
('p', 't')
('p', 'h')
('p', 'o')
('p', 'n')
('y', 'p')
('y', 't')
('y', 'h')
('y', 'o')
('y', 'n')
('t', 'p')
('t', 'y')
('t', 'h')
('t', 'o')
('t', 'n')
('h', 'p')
('h', 'y')
('h', 't')
('h', 'o')
('h', 'n')
('o', 'p')
('o', 'y')
('o', 't')
('o', 'h')
('o', 'n')
('n', 'p')
('n', 'y')
('n', 't')
('n', 'h')
('n', 'o')
Working with Combinations
Basic Combinations
Use itertools.combinations() to get combinations of a specific length ?
from itertools import combinations
letters = ['p', 'y', 't', 'h', 'o', 'n']
combi = combinations(letters, 3)
for c in combi:
print(c)
('p', 'y', 't')
('p', 'y', 'h')
('p', 'y', 'o')
('p', 'y', 'n')
('p', 't', 'h')
('p', 't', 'o')
('p', 't', 'n')
('p', 'h', 'o')
('p', 'h', 'n')
('p', 'o', 'n')
('y', 't', 'h')
('y', 't', 'o')
('y', 't', 'n')
('y', 'h', 'o')
('y', 'h', 'n')
('y', 'o', 'n')
('t', 'h', 'o')
('t', 'h', 'n')
('t', 'o', 'n')
('h', 'o', 'n')
Combinations with Replacement
Use combinations_with_replacement() to allow repeated elements ?
from itertools import combinations_with_replacement
letters = ['p', 'y', 't']
combi = combinations_with_replacement(letters, 2)
for c in combi:
print(c)
('p', 'p')
('p', 'y')
('p', 't')
('y', 'y')
('y', 't')
('t', 't')
Practical Example
Here's a practical example counting permutations and combinations ?
from itertools import permutations, combinations
data = [1, 2, 3, 4]
# Count permutations of length 2
perm_count = len(list(permutations(data, 2)))
print(f"Permutations of length 2: {perm_count}")
# Count combinations of length 2
comb_count = len(list(combinations(data, 2)))
print(f"Combinations of length 2: {comb_count}")
Permutations of length 2: 12 Combinations of length 2: 6
Conclusion
Python's itertools module makes generating permutations and combinations simple. Use permutations() when order matters and combinations() when it doesn't. Both functions accept a length parameter for specific-sized arrangements.
