Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

itertools.product

Generates the Cartesian product with the rightmost input advancing fastest.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.product(*iterables, repeat=1)

Generates the Cartesian product with the rightmost input advancing fastest.

Taking zero factors yields one empty tuple. For positive repeat, input pools are materialized before products are produced.

The Cartesian product selects one item per factor. The rightmost pool cycles fastest, so a pairs with 0 and 1 before b does. With positive repeat, input pools are collected first and must be finite. With repeat zero, the inputs are never read. Zero factors have one choice with no components, the empty tuple.

import itertools as I
def unread():
    raise AssertionError
    yield
assert list(I.product(unread(), repeat=0)) == [()]
assert list(I.product("a", [])) == []

Examples

import itertools as I
print(list(I.product("ab", range(2))))
print(list(I.product("ab", repeat=0)))

Result

[('a', 0), ('a', 1), ('b', 0), ('b', 1)]
[()]

The Cartesian product selects one item per factor. The rightmost pool cycles fastest, so a pairs with 0 and 1 before b does. With positive repeat, input pools are collected first and must be finite. With repeat zero, the inputs are never read. Zero factors have one choice with no components, the empty tuple.

Sources