On this page
  1. itertools.product
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

itertools.product

Prev: itertools.permutations | Up: itertools | Next: itertools.repeat
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.product
Tags: #public #python3 #stdlib #itertools


Import

import itertools
from itertools import product

Signature & Overview

itertools.product(*iterables, repeat=1)

Cartesian product of input iterables. Equivalent to nested for-loops.

Usage

from itertools import product

assert list(product("AB", [1, 2])) == [("A", 1), ("A", 2), ("B", 1), ("B", 2)]
# repeat= produces the Cartesian power (like nested loops over the same iterable)
assert list(product([0, 1], repeat=2)) == [(0, 0), (0, 1), (1, 0), (1, 1)]

Gotchas

REPL Inspect

from itertools import product
help(product)

Linked from

Related notes