Itertools.Product() - Python
Last Updated :
14 Apr, 2025
The product() function from Python's built-in itertools module is a powerful tool that returns the Cartesian product of input iterables. This means it produces all possible combinations of the elements, where the result is similar to a nested for-loop. Example:
Python
from itertools import product
print(list(product([1, 2], [3, 4])))
Output[(1, 3), (1, 4), (2, 3), (2, 4)]
Explanation: This code pairs each element of the first list with all elements of the second list, combining 1 with 3 and 4, and 2 with 3 and 4. Wrapping the product() result with list() converts the output into a list of tuples for display.
Syntax of itertools.product()
itertools.product(*iterables, repeat=1)
Parameters:
- *iterables: One or more iterable objects (e.g., list, tuple, string).
- repeat (Optional): The number of repetitions of the iterable. Default is 1.
Returns: It returns an iterator that yields tuples with all possible combinations, in the order of a nested loop (left to right).
Examples of itertools.product()
Example 1: In this example, we are using the repeat parameter of the product() function to generate the Cartesian product of the list [0, 1] repeated 3 times.
Python
from itertools import product
print(list(product([0, 1], repeat=3)))
Output[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
Explanation: product() with repeat=3 to generate all 3-length binary combinations from the list [0, 1]. It computes the Cartesian product [0, 1] x [0, 1] x [0, 1], resulting in tuples like (0, 0, 0) to (1, 1, 1) that represent every possible sequence of three binary digits.
Example 2: In this example, we are passing two strings ("AB" and "CD") to the product() function. Since strings are iterable in Python, each character in the string is treated as an individual element:
Python
from itertools import product
print(list(product("AB", "CD")))
Output[('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D')]
Explanation: This code generates all possible pairs by combining each character from "AB" with each character from "CD", resulting in tuples like ('A', 'C'), ('A', 'D'), ('B', 'C') and ('B', 'D').
Example 3: In this example, we are using the product() function to compute the Cartesian product of two different lists using for loop.
Python
from itertools import product
for i in product([1, 2], ['a', 'b']):
print(i)
Output(1, 'a')
(1, 'b')
(2, 'a')
(2, 'b')
Explanation: This code generates all possible pairs by combining each element from the first list with each element from the second list. The for loop iterates through these pairs and prints them, resulting in the tuples (1, 'a'), (1, 'b'), (2, 'a') and (2, 'b').
Similar Reads
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
12 min read
Python - Itertools.count()
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
3 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
3 min read
Python - itertools.repeat()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Itertools.accumulate()-Python
itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
4 min read
Python - Itertools.chain.from_iterable()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th
2 min read
Python - Itertools.compress()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform
1 min read
Python - Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
2 min read