# Python Itertools.product() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and space efficiency. itertools enable us to solve complex problems quickly and easily. Iterators are classified into three types.

This module provides the following types of iterators:

1. Combinatorics Generators
2. Infinite Iterators
3. Terminating Iterators

itertools.product() Function:

Cartesian Product of two sets is defined in mathematics as the set of all ordered pairs (a, b) where a belongs to A and b belongs to B.

itertools.product() belongs to the Python itertools library’s Combinatoric iterators category.

itertools.product() returns the cartesian product from the specified iterator, with the output being lexicographically sorted.

There are two ways to use itertools.product():

itertools.product(*iterables, repeat=1):

This function returns the cartesian product of the provided iterable with itself for the number of times specified by the optional keyword “repeat.”

For example, product(arr, repeat=4) is equivalent to product (arr, arr, arr, arr).
itertools.product(*iterables):

This function returns the cartesian product of all iterables passed as arguments. For instance, product (arr1, arr2, arr3, arr4).

Consider the following example to gain a better idea.

Examples:

Example1:

Input:

Given first List = [10, 9, 8]
Given second List = [12, 3, 40]

Output:

The cartesian product of the given two lists =
[(10, 12), (10, 3), (10, 40), (9, 12), (9, 3), (9, 40), (8, 12), (8, 3), (8, 40)]

Example2:

Input:

Given first List = [78, 2, 6]
Given second List = [4, 2, 7]

Output:

The cartesian product of the given two lists =
[(78, 4), (78, 2), (78, 7), (2, 4), (2, 2), (2, 7), (6, 4), (6, 2), (6, 7)]

## Itertools.product() Function with Examples in Python

### Method #1: Using Built-in Functions (Static Input)

Approach:

• Import product() function from itertools module using the import keyword.
• Create a function say CartesianProdct() which accepts the two lists as the arguments and returns the cartesian product of the given two lists.
• Inside the function, Using the product() method, return a list of all computed tuples.
• Inside the main function, Give the first list as static input and store it in a variable.
• Give the second list as static input and store it in another variable.
• Pass the given two lists as the arguments to the above declared CartesianProdct() function and store it in another variable.
• Print the cartesian product of the given two lists.
• The Exit of the Program.

Below is the implementation:

# Import product() function from itertools module using the import keyword.
from itertools import product

# Create a function say CartesianProdct() which accepts the two lists as
# the arguments and returns the cartesian Product of the given two lists

def CartesianProdct(gvn_fstlst, gvn_scndlst):

# Using the product() method, return a list of all computed tuples.
return list(product(gvn_fstlst, gvn_scndlst))

# Inside the main function,
# Give the first list as static input and store it in a variable.
gvn_fstlst = [10, 9, 8]
# Give the second list as static input and store it in another variable.
gvn_scndlst = [12, 3, 40]
# Pass the given two lists as the arguments to the above declared CartesianProdct()
# function and store it in another variable.
rslt = CartesianProdct(gvn_fstlst, gvn_scndlst)
# Print the cartesian product of the given two lists
print("The cartesian product of the given two lists = ")
print(rslt)

Output:

The cartesian product of the given two lists =
[(10, 12), (10, 3), (10, 40), (9, 12), (9, 3), (9, 40), (8, 12), (8, 3), (8, 40)]

### Method #2: Using Built-in Functions (User Input)

Approach:

• Import product() function from itertools module using the import keyword.
• Create a function say CartesianProdct() which accepts the two lists as the arguments and returns the cartesian product of the given two lists.
• Inside the function, Using the product() method, return a list of all computed tuples.
• Inside the main function,
• Give the first list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Give the second list as user input using list(),map(),input(),and split() functions.
• Store it in another variable.
• Pass the given two lists as the arguments to the above declared CartesianProdct() function and store it in another variable.
• Print the cartesian product of the given two lists.
• The Exit of the Program.

Below is the implementation:

# Import product() function from itertools module using the import keyword.
from itertools import product

# Create a function say CartesianProdct() which accepts the two lists as
# the arguments and returns the cartesian Product of the given two lists

def CartesianProdct(gvn_fstlst, gvn_scndlst):

# Using the product() method, return a list of all computed tuples.
return list(product(gvn_fstlst, gvn_scndlst))

# Inside the main function,
# Give the first list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_fstlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_scndlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Pass the given two lists as the arguments to the above declared CartesianProdct()
# function and store it in another variable.
rslt = CartesianProdct(gvn_fstlst, gvn_scndlst)
# Print the cartesian product of the given two lists
print("The cartesian product of the given two lists = ")
print(rslt)

Output:

Enter some random List Elements separated by spaces = 78 2 6
Enter some random List Elements separated by spaces = 4 2 7
The cartesian product of the given two lists =
[(78, 4), (78, 2), (78, 7), (2, 4), (2, 2), (2, 7), (6, 4), (6, 2), (6, 7)]