Python itertools: Permutations vs Combinations

Python's itertools module provides specialized functions for combinatorial computing, most notably permutations() and combinations(). While both functions take an input collection and generate subsets of a specified length, the key distinction lies in whether the order of elements matters. permutations() treats different orderings of the same items as unique outcomes, whereas combinations() considers order irrelevant, returning only unique groupings regardless of sequence.

The Core Difference: Order Matters vs. Order Does Not Matter

The fundamental distinction between these two functions comes down to mathematics:

Neither function allows an element at a specific index to be repeated with itself unless the input iterable contains duplicate values.


How itertools.permutations() Works

itertools.permutations(iterable, r=None) returns successive \(r\)-length permutations of elements from the provided iterable.

import itertools

data = ['A', 'B', 'C']

# Generate 2-element permutations
result = list(itertools.permutations(data, 2))
print(result)

Output:

[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]

Notice that both ('A', 'B') and ('B', 'A') are present in the output.


How itertools.combinations() Works

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

import itertools

data = ['A', 'B', 'C']

# Generate 2-element combinations
result = list(itertools.combinations(data, 2))
print(result)

Output:

[('A', 'B'), ('A', 'C'), ('B', 'C')]

Because order does not matter, ('B', 'A') is omitted because its items are already represented by ('A', 'B').


Direct Comparison

Feature itertools.permutations() itertools.combinations()
Order Significance Order matters Order does not matter
Duplicate Sets Yields both (x, y) and (y, x) Yields only (x, y)
Length Argument (r) Optional (defaults to length of input) Required
Result Count Formula \(P(n, r) = \frac{n!}{(n-r)!}\) \(C(n, r) = \frac{n!}{r!(n-r)!}\)
Size of Output Always greater than or equal to combinations Always smaller than or equal to permutations

When to Use Which