ML Foundations

Python itertools.product consumes inputs before yielding

itertools.product returns tuples lazily, but first stores each input iterable in memory. Do not pass an unbounded generator expecting streaming Cartesian products.

Example results for Python itertools.product consumes inputs before yielding. input after product call: 'started', 'exhausted'
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Product outputs are lazy, but its inputs are not streamed

`itertools.product` is useful for nested-loop combinations, and it returns an iterator of result tuples. That output iterator can make the operation look streaming from end to end. The important memory detail is that `product` first completely consumes each input iterable and keeps the resulting pools so it can generate combinations. [1]

This means a large finite input may be stored before the first result appears, and an infinite input never finishes being collected. Calling `next` on the result does not change that setup behavior. Do not feed `product` an unbounded generator under the assumption that it will emit combinations as values arrive. [1]

References: [1]

Observe input consumption separately from result iteration

The example uses a small generator that logs when it starts and when it is exhausted. Immediately after calling `product`, the log already contains both events, even though no pair has been requested. The first call to `next` then returns one tuple from the stored pools. Later tuples can be generated without advancing the source generator again. [1]

Python defines an iterator as an object that produces successive values when requested through the iterator protocol. That describes how the returned product iterator yields results, but it does not imply that each source iterator remains unconsumed. The two stages are separate: input pools are materialized first, then result tuples are produced on demand. [1, 2]

Executed with Python 3.12.14; assertions passed.

"""Show that itertools.product consumes finite inputs before yielding tuples."""
from itertools import product

log = []
def axis():
    log.append("started")
    yield "red"
    yield "blue"
    log.append("exhausted")

pairs = product(axis(), [1, 2])
print("input after product call:", log)
print("first pair:", next(pairs))
print("input after first result:", log)

assert log == ["started", "exhausted"]
assert next(pairs) == ("red", 2)
assert next(pairs) == ("blue", 1)

Recorded output

input after product call: ['started', 'exhausted']
first pair: ('red', 1)
input after first result: ['started', 'exhausted']

References: [1], [2]

Use a different pattern when inputs must stay streaming

When a Cartesian product is too large to hold its input pools, reduce the input domain, process combinations in bounded chunks, or use nested iteration tailored to the application. If one dimension is unbounded, an ordinary full Cartesian product is not a finite job; define a stopping rule before combining it. [1]

This example proves only the documented eager input consumption for two tiny finite iterables. It does not benchmark memory, estimate product cardinality, or claim that every iterator recipe has the same behavior. The practical check is to separate input preparation from output generation and count both the sizes and lifetime of the pools you are asking `product` to retain. [1, 2]

References: [1], [2]

Sources

  1. Python: itertools.product
  2. Python glossary: iterator