itertools.product() / itertools.permutations() / itertools.combinations()
itertools.product() generates the Cartesian product of multiple iterables. itertools.permutations() generates permutations (ordered, no repetition), and itertools.combinations() generates combinations (unordered, no repetition). These functions use lazy evaluation and do not hold results in memory, so they can handle a large number of combinations efficiently.
Syntax
import itertools # Cartesian product itertools.product(*iterables, repeat=1) # Permutations (no repetition) itertools.permutations(iterable, r=None) # Combinations (no repetition) itertools.combinations(iterable, r) # Combinations (with repetition) itertools.combinations_with_replacement(iterable, r)
Function List
| Function | Description |
|---|---|
| product(*iterables, repeat=1) | Generates the Cartesian product of multiple iterables as tuples. Use repeat to repeat the same iterable multiple times. |
| permutations(iterable, r) | Generates all permutations of r elements. If r is omitted, permutations of all elements are generated. |
| combinations(iterable, r) | Generates all combinations of r elements (no repetition). |
| combinations_with_replacement(iterable, r) | Generates all combinations of r elements (with repetition allowed). |
Sample Code
import itertools
# product: Cartesian product (all combinations)
sizes = ['S', 'M', 'L']
colors = ['red', 'blue']
result = list(itertools.product(sizes, colors))
print(result)
# [('S', 'red'), ('S', 'blue'), ('M', 'red'), ('M', 'blue'), ('L', 'red'), ('L', 'blue')]
# product: Cartesian product of the same list using repeat (two dice)
dice = list(itertools.product(range(1, 7), repeat=2))
print(len(dice)) # 36 (all 6x6 combinations)
print(dice[:3]) # [(1, 1), (1, 2), (1, 3)]
# permutations: permutations (ordered, no repetition)
letters = ['A', 'B', 'C']
perms = list(itertools.permutations(letters, 2))
print(perms)
# [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
# permutations: permutations of all elements
all_perms = list(itertools.permutations([1, 2, 3]))
print(len(all_perms)) # 6 (3! = 6)
# combinations: combinations (unordered, no repetition)
combs = list(itertools.combinations([1, 2, 3, 4], 2))
print(combs)
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
# combinations_with_replacement: combinations with repetition allowed
combs_rep = list(itertools.combinations_with_replacement(['A', 'B', 'C'], 2))
print(combs_rep)
# [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
Notes
All of these functions return an iterator of tuples. Be aware that as the number of elements grows, the number of generated combinations increases exponentially. For example, the number of permutations choosing r from n elements is P(n,r) = n!/(n-r)!, and the number of combinations is C(n,r) = n!/(r!(n-r)!).
The repeat option of product() lets you efficiently generate the Cartesian product of the same iterable used multiple times. You can achieve the same result with nested for loops, but using product results in more concise code. These functions are useful for brute-force searches and automatic test case generation.
If you find any errors or copyright issues, please contact us.