SyntaxStudy
Sign Up
Python Intermediate 3 min read

itertools.product

itertools.product

itertools.product() generates the Cartesian product of input iterables — equivalent to nested for loops.

Example
from itertools import product

for combo in product("AB", [1, 2]):
    print(combo)
# ("A", 1) ("A", 2) ("B", 1) ("B", 2)
Pro Tip

Use repeat= to repeat the same iterable: product("AB", repeat=2).