 # 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

## Python – Add two list elementsPython – Add two list elements

Python | Adding two list elements There can be many situations in which one requires to find index wise summation of two different lists. This can have a possible applications in day-day programming. Lets discuss various ways in which this

## Add multiple columns to dataframe in PandasAdd multiple columns to dataframe in Pandas

Add multiple columns to dataframe in Pandas In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to the Pandas data frame. Method 1: Add multiple columns to

## Get a specific row in a given Pandas DataFrameGet a specific row in a given Pandas DataFrame

Get a specific row in a given Pandas DataFrame In the Pandas DataFrame  we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter. pandas.DataFrame.iloc[] Syntax : pandas.DataFrame.iloc[] Parameters

## Array Copying in PythonArray Copying in Python

Array Copying in Python Let us see how to copy arrays in Python. There are 3 ways to copy arrays : Simply using the assignment operator. Shallow Copy Deep Copy Assigning the Array We can create a copy of an

## Python | Pandas dataframe.shift()Python | Pandas dataframe.shift()

Pandas dataframe.shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called period, which represents the number of shifts to be made over the desired axis. This function is very

## Convert Text file to JSON in PythonConvert Text file to JSON in Python

Convert Text file to JSON in Python In Python, converting a text file to JSON (JavaScript Object Notation) format is a common task for transmitting data between web applications and servers. Luckily, the process can be made easy with the