# Python – Itertools.Product()

## Python – Itertools.Product()

In the terms of Mathematics Cartesian Product of two sets is defined as the set of all ordered pairs (a, b) where a belongs to A and b belongs to B. Consider the below example for better understanding.
Examples:

Input : arr1 = [1, 2, 3]
arr2 = [5, 6, 7]
Output : [(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
Input : arr1 = [10, 12]
arr2 = [8, 9, 10]
Output : [(10, 8), (10, 9), (10, 10), (12, 8), (12, 9), (12, 10)]

The above solution can be done by looping but we will use a special Python library itertools.product() for finding the Cartesian Product. Let’s go through the working and use cases of this Python library.

#### What are Itertools in Python?

Python Itertools is a library in Python which consists of multiple methods that are used in various iterators to compute a fast and code efficient solution.

itertools.product() falls under the category called Combinatoric iterators of the Python itertools library.

#### What does itertools.product() do?

itertools.product() is used to find the cartesian product from the given iterator, output is lexicographic ordered. The itertools.product() can used in two different ways:

• itertools.product(*iterables, repeat=1):
It 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=3) means the same as product(arr, arr, arr).
• itertools.product(*iterables):
It returns the cartesian product of all the iterable provided as the argument. For example, product(arr1, arr2, arr3).

Example:

 `from` `itertools ``import` `product` `def` `cartesian_product(arr1, arr2):` `    ``# return the list of all the computed tuple` `    ``# using the product() method` `    ``return` `list``(product(arr1, arr2))` `  ` `# Driver Function` `if` `__name__ ``=``=` `"__main__"``:` `    ``arr1 ``=` `[``1``, ``2``, ``3``]` `    ``arr2 ``=` `[``5``, ``6``, ``7``]` `    ``print``(cartesian_product(arr1, arr2))`

Output:

[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]

Last Updated on November 1, 2021 by admin

## Min Heap in PythonMin Heap in Python

Min Heap in Python A Min-Heap is a complete binary tree in which the value in each

## Difference between List and Array in PythonDifference between List and Array in Python

Difference between List and Array in Python List: A list in Python is a collection

## Python 3 – input() functionPython 3 – input() function

Python 3 – input() function In Python, we use input() function to take input from the user.

## Implementing Apriori algorithm in PythonImplementing Apriori algorithm in Python

Implementing Apriori algorithm in Python Apriori Algorithm is a Machine Learning algorithm which is used

## Python PIL getpixel() MethodPython PIL getpixel() Method

Python PIL | getpixel() Method PIL is the Python Imaging Library which provides the python

## Convert the column type from string to datetime format in Pandas dataframeConvert the column type from string to datetime format in Pandas dataframe

In this tutorial, we will explore how to convert a column of string values to

## Complex Numbers in PythonComplex Numbers in Python

Complex Numbers in Python Not only real numbers, Python can also handle complex numbers and

## Python ord() functionPython ord() function

ord() function in Python Python ord() function returns the Unicode code from a given character. This