Permutations and Combinations in Python (Itertools)

Extracted from itertools — Functions creating iterators for efficient looping — Python 3.11.3 documentation

PERMUTATIONS

itertools.permutations(iterable, r=None) returns successive r length permutations of elements in the iterable.

If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Ex.1.

1
2
3
4
5
6
7
from itertools import permutations

a = [1, 2, 3]

for p in permutations(a):
print(p)

Output:

1
2
3
4
5
6
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

Ex.2.

1
2
3
4
5
6
from itertools import permutations

a = ['A', 'B', 'C', 'D']

for p in permutations(a, 2):
print(p)

Output:

1
2
3
4
5
6
7
8
9
10
11
12
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'A')
('B', 'C')
('B', 'D')
('C', 'A')
('C', 'B')
('C', 'D')
('D', 'A')
('D', 'B')
('D', 'C')

Learn more here

COMBINATIONS

Notice: r is NOT optional. (Not like permutations)

WITH NO REPLACEMENTS

itertools.combinations(iterable, r) returns r length subsequences of elements from the input iterable.

Ex.

1
2
3
4
5
6
from itertools import combinations

a = [1, 2, 3]

for c in combinations(a, 2):
print(c)

Output:

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

Learn more here

WITH REPLACEMENTS

itertools.combinations_with_replacement(iterable, r) returns r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.

Ex.

1
2
3
4
5
6
from itertools import combinations_with_replacement

a = [1, 2, 3]

for c_with_replacement in combinations_with_replacement(a, 2):
print(c_with_replacement)

Output:

1
2
3
4
5
6
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)

Learn more here