How to Generate Cartesian Product Combinations in Python
Use itertools.product to generate every combination across multiple iterables, a pattern common for product variant generation.
Python code
17 linesfrom itertools import product
def generate_cartesian_combinations(*iterables):
"""Generate all Cartesian product combinations of given iterables."""
return list(product(*iterables))
if __name__ == "__main__":
colors = ["red", "green", "blue"]
sizes = ["S", "M", "L"]
styles = ["t-shirt", "hoodie"]
combinations = generate_cartesian_combinations(colors, sizes, styles)
for combo in combinations:
print(combo)
print(f"\nTotal combinations: {len(combinations)}")
Output
('red', 'S', 't-shirt')
('red', 'S', 'hoodie')
('red', 'M', 't-shirt')
('red', 'M', 'hoodie')
('red', 'L', 't-shirt')
('red', 'L', 'hoodie')
('green', 'S', 't-shirt')
('green', 'S', 'hoodie')
('green', 'M', 't-shirt')
('green', 'M', 'hoodie')
('green', 'L', 't-shirt')
('green', 'L', 'hoodie')
('blue', 'S', 't-shirt')
('blue', 'S', 'hoodie')
('blue', 'M', 't-shirt')
('blue', 'M', 'hoodie')
('blue', 'L', 't-shirt')
('blue', 'L', 'hoodie')
Total combinations: 18
How it works
itertools.product computes the Cartesian product by taking every possible ordered pairing of elements across the input iterables, so the result length is the product of each input's length (3 × 3 × 2 = 18). The *iterables syntax unpacks any number of passed arguments into separate positional arguments, making the function flexible for any count of attribute lists. Wrapping the result in list() materializes the lazy generator into a concrete list that can be reused or indexed. This approach avoids nested loops entirely, keeping the code concise and the logic explicit. Because product yields tuples, you can destructure each combination directly in the loop for building SKUs or rendering product UI.
Common mistakes
- Forgetting that `product` returns an iterator, not a list, so you must wrap it if you need repeated access
- Confusing `product` with `permutations` or `combinations`, which solve different selection problems
- Passing a single list like `product(colors)` when you meant `product(colors, sizes)` — it only takes multiple iterables
- Assuming `product` deduplicates identical values — it doesn't; it generates every ordered pairing
Variations
- Use `itertools.product.repeat` for repeated values: `product(colors, repeat=2)`
- Lazily iterate with `for combo in product(*iterables):` to avoid holding all tuples in memory
Real-world use cases
- Generating all SKU combinations for an e-commerce catalog from color, size, and material attributes.
- Running parameter sweeps in machine learning where each hyperparameter combination gets tested.
- Building a test matrix for integration tests that must run every input permutation across multiple services.
Sponsored
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a Generator Pipeline in Python: Filter Then Map medium
- Build a lazy generator to read file lines in Python easy
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
Keep learning
Related tutorials and quizzes for this topic.