Python

# Itertools.product

Itertools is a package that offers different methodologies in Python. Iterating over attributes is made easier by using these methods. The itertools package’s product() function produces the cartesian product of the provided lists or sets. The repeat parameter, which is an additional part of the function, provides the value’s Cartesian combination with itself over the iterations indicated in the repeat argument. Let’s demonstrate the methods of using the itertools module’s product() function.

Example 1:
In this example, we create two lists and then find the Cartesian product of that defined lists.

from itertools import product

List = [21, 44, 63, 74, 15]
print("Actual list is :", List)
print("The cartesian product of that list: ", list(product(List, repeat=3)))

List_1 = [51, 12, 23]
List_2 = ['xa', 'yb', 'zc']
print("The cartesian product of both lists:", list(product(List_1, List_2)))

We start the code by integrating the product() method that is associated with the itertools framework. Then, we indicate some elements in the form of a list. These values are stored in a “List” variable. Now, we utilize the print() function to display the actual list. The product() function consists of two arguments which include the list and the “repeat” attribute. The value of this attribute is set to be 3 here to get the Cartesian product of the defined list with itself. This product of the list is shown on the screen by using the function print().

Furthermore, we create two new lists. The first list contains three numerical values and the second list contains the alphabetical sets. Now, we call the product() method. We pass both lists as the arguments of the function. By doing so, we find the Cartesian product between these two lists. The print() method is used to display the Cartesian product.

Example no 2:
Here, we utilize the cartesian_product() method to acquire the Cartesian product of two arrays.

from itertools import product

def cartesian_product(a_1, a_2):

return list(product(a_1, a_2))
if __name__ == "__main__":
a_1 = [101, 938, 854]
a_2 = [370, 691, 287]
print(cartesian_product(a_1, a_2))

We incorporate the product library from the itertools package. Then, we define the Cartesian_product() function. Within this function, we pass two arrays as the parameter of the function. This method returns the values of both arrays by taking the product of the values. We also utilize the product() method within the return statement.

Now, it’s time to call the driver function. Inside the body of the main function, we create two arrays and define some random values in these arrays. We call the Cartesian_product() function to acquire the Cartesian product of those mentioned arrays. We utilize the print() function to depict their product.

Example no 3:
In this illustration, for simplicity and efficiency, the product() function is utilized in place of the for loop.

import time
import itertools

l_1 = [4,8,2]
l_2 = [1,7,3]
l_3 = [5,6,2]

s_1 = time.time()
iter_list = itertools.product(l_1,l_2,l_3)
a_1 = time.time()

print("Outcome of the itertools.product() funtion:")
print(list(iter_list))

list_new = []
s_2 = time.time()
for l in l_1:
for m in l_2:
for n in l_3:
list_new.append((l,m,n))
a_2 = time.time()

print("Outcome by using FOR loop:")
print(list_new)

print(f"Time taken for obtaining the itertools.product():{a_1-s_1}")
print(f"Time taken for the use of 'for' loop:{a_2-s_2}")

To begin the program, we introduce two required modules. The “time” framework is integrated to determine the speediness of the executions and the “itertools” module is imported to deal with the product() functionality and for a loop. Next, we declare three variables which include “l_1”, “l_2”, and “l_3”. These variables are used to store different values. All these lists contain three digits.

To acquire the rapidness of the implementation, we call the time() method. The calculated time is stored in a “s_1” variable. This method is taken from the time header file. In the next step, we utilize the product() method of the itertools package. We pass the three defined lists as the arguments of the product function. We call the print() method to display the product of the lists by using the product() method.

Along with this, we utilize the print() method to print the list. Now let’s initialize a new variable termed “list_new”. This list is set as empty. We use the time() function to determine the efficiency of the system implementation. The “s_2” variable contains the determined time. The time header file is where this methodology is adopted. It excludes the l_3 for the values of n 2 & 3 in the first execution of the “for” loop with l = 4, m = 8, and n = 2. The process is repeated while specifying the m = 7. It stops the l_3 for the values of the parameters k 2 & 3 in the second series of the “for” loop having l = 1, m = 7, and n = 3. The process is then repeated when setting the m = 7. For l=5, this is executed accordingly.

We utilize the append() method and pass the three iterations as its parameters since we add the outcome of these iterations. We call the time() approach to evaluate how quickly these iterations occur. The variable “a_2” holds the estimated time. Now, we used the print() function to present the product of the lists by applying a “for” loop.

In addition, we show the list using the print() function. To display the calculated times for both procedures, we ultimately invoke the print() method. The itertools product() function and for loop iterations’ relative starting and ending times are shown by the variables s_1, s_2, and a_1 and a_2, respectively. The execution times of the for loop and product() function are presented as a_1 – s_1 and a_2 – s_2, correspondingly.

## Conclusion

In this article, we talked about how to utilize the product() method of the itertools module in Python. Users can traverse through the Cartesian product of a set of iterables with this method. The code is simplified and effective by using the product() method. In the first example of this guide, we created a list and obtained the Cartesian product of the list itself. In another instance, we declared two arrays and got the Cartesian product of those arrays. We used the product() function instead of the “for” loop.

#### Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content