itertools.product(*iterables, repeat=1) returns the Cartesian product of its inputs: every possible tuple that takes one element from each iterable. It is the flat replacement for nested for loops, and repeat=n takes the product of a single iterable with itself n times.
The result is a lazy iterator, so combinations are produced one at a time instead of being stored in memory.
repeat argumentEach result is a tuple with one item per input iterable. The rightmost iterable advances fastest, exactly like the innermost loop of nested for statements. The code above is equivalent to:
The number of results is the product of the input lengths: 2 colors times 3 sizes gives 6 tuples.
repeat argumentrepeat multiplies the input with itself. It is the clean way to enumerate fixed-length sequences, such as binary strings or dice rolls.
product(x, repeat=3) is the same as product(x, x, x). Unlike permutations, elements may repeat, which is why there are len(x) ** repeat results here (2 to the power 3 gives 8).
Flat code is easier to read and lets you vary the number of dimensions without rewriting loops.
Grid search over configuration values is a typical case. With pytest you can also feed product(...) into @pytest.mark.parametrize.
This prints all 8 configurations as dictionaries. Unpacking with *params.values() passes each list as its own argument.
The generator expression keeps memory flat even though there are 676 two-letter codes. For longer lengths the count explodes, so apply it only to small search spaces.
list(product([1, 2], [])) returns [].list(product()) returns [()], a single empty tuple.product reads each input iterable fully into a tuple before it yields anything, so infinite iterators such as itertools.count() will not work.list() first, or build it fresh.math.prod(len(x) for x in inputs) before iterating.Note that math.prod() multiplies numbers and is unrelated to itertools.product, despite the similar name.
| Function | Meaning | "AB", length 2 |
|---|---|---|
product(s, repeat=2) | Ordered, repeats allowed | AA AB BA BB |
permutations(s, 2) | Ordered, no repeats | AB BA |
combinations(s, 2) | Unordered, no repeats | AB |
combinations_with_replacement(s, 2) | Unordered, repeats allowed | AA AB BB |
itertools.product return?A lazy iterator of tuples. Convert it with list() if you need all values at once, but only when the result is small.
product different from zip?zip pairs items by position and stops at the shortest input. product pairs every item of one iterable with every item of the others.
product work with a single iterable?Yes. Use repeat, for example product("ab", repeat=2). Without repeat, a single input yields one-element tuples.
product guarantee an order?Yes. The order is deterministic: the rightmost input varies fastest, like nested loops.
itertools.product generates the Cartesian product of iterables as tuples, replaces nested loops with one flat loop, and supports repeat for self-products. Remember that results multiply quickly, that inputs are read fully first, and that the iterator is lazy.