itertools.product()

Itertools

Computes the Cartesian product of input iterables (like nested for loops).

Signature

itertools.product(*iterables, repeat=1)

Returns

product

Example

from itertools import product
colors = ['red', 'blue']
sizes = ['S', 'M', 'L']
for c, s in product(colors, sizes):
    print(f'{c}-{s}')

About itertools.product()

itertools.product is a Python itertools function with the signature itertools.product(*iterables, repeat=1). Computes the Cartesian product of input iterables (like nested for loops). It returns a value of type product.

Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The itertools.productfunction is commonly used in data processing, web development, scripting, and automation tasks.

When working with itertools.product(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.

Related Functions