Python itertools.product(): Cartesian Product Examples

itertools.product() returns the Cartesian product of input iterables. In practical Python code, it is a compact way to replace nested loops when you need every combination of values.

from itertools import product

for combo in product(iterable_a, iterable_b):
    print(combo)

The result is an iterator of tuples. Convert it to a list only when you actually need all combinations in memory.

Basic itertools.product() example

Pass two or more iterables to generate every ordered combination.

from itertools import product

colors = ["red", "blue"]
sizes = ["S", "M", "L"]

print(list(product(colors, sizes)))

Output starts with ('red', 'S'), then ('red', 'M'), and continues until every color and size pair has been produced.

Equivalent nested loop

product(colors, sizes) is equivalent to a nested loop where the rightmost iterable changes fastest.

colors = ["red", "blue"]
sizes = ["S", "M"]

pairs = [(color, size) for color in colors for size in sizes]
print(pairs)

For two short lists, either style is readable. product() becomes more useful when there are several dimensions.

Use repeat for the same iterable

The repeat argument repeats the same iterable multiple times. This is useful for binary states, grids, and search spaces.

from itertools import product

print(list(product([0, 1], repeat=3)))

product([0, 1], repeat=3) is the same as product([0, 1], [0, 1], [0, 1]).

Use repeat only when the same iterable should be reused. If each position has a different set of choices, pass each iterable separately, such as product(colors, sizes, materials).

Loop lazily instead of building a list

product() returns an iterator, so you can loop over combinations without immediately building a list.

from itertools import product

for row, column in product(range(2), range(3)):
    print(row, column)

This is the right pattern when each combination can be processed one at a time.

Filter combinations

You can filter product results with a comprehension or a normal loop.

from itertools import product

pairs = product(range(4), repeat=2)
filtered = [(x, y) for x, y in pairs if x < y]
print(filtered)

This example keeps only pairs where the first number is smaller than the second.

How many combinations will product() create?

The number of output tuples is the product of the input lengths. For lengths 2, 3, and 4, there are 24 combinations.

from math import prod

sizes = [2, 3, 4]
print(prod(sizes))

Check this before calling list(product(...)). A few input lists can create a very large result.

What happens with an empty iterable?

If any input iterable is empty, the Cartesian product is empty.

from itertools import product

print(list(product([], [1, 2])))

Output:

[]

product() vs zip()

zip(a, b) pairs items by position: first with first, second with second. product(a, b) returns every possible pair. Use zip() for parallel data and product() for combinations.

Practical use case: parameter grids

A common use for product() is building a small parameter grid. Each tuple represents one combination to test.

from itertools import product

learning_rates = [0.01, 0.1]
depths = [3, 5]

for learning_rate, depth in product(learning_rates, depths):
    print({"learning_rate": learning_rate, "depth": depth})

This avoids manually nesting loops for every parameter. Before using this pattern with many options, estimate the total count so the grid does not become unexpectedly large.

Common mistakes

  • Converting huge products to a list: loop over the iterator when possible.
  • Expecting zip behavior: product() creates every combination, not positional pairs.
  • Forgetting tuple output: each result is a tuple, even if the inputs are lists or strings.
  • Using infinite inputs: Python docs note that product() consumes input iterables into pools before running, so it is only useful with finite inputs.
  • Forgetting repeat: use repeat=n when combining an iterable with itself.

Related Python guides

Official reference

Conclusion

Use itertools.product() when you need every Cartesian-product combination from finite inputs. It is cleaner than deeply nested loops, but the number of combinations can grow quickly, so avoid converting very large products to a list.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted