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.

Easy Python 3.9+ Aug 9, 2026 Comprehensions & generators 13 views 0 copies

Python code

17 lines
Python 3.9+
from 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

stdout
('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

  1. Use `itertools.product.repeat` for repeated values: `product(colors, repeat=2)`
  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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Comprehensions & generators

Related tutorials and quizzes for this topic.