Python | Column Product in List of lists
Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using loop + list comprehension + zip()
The combination of above methods are required to solve this particular problem. The explicit product function is used to get the required product value and zip function provides the combination of like indices and then list is created using list comprehension.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
print ( "The original list : " + str (test_list))
res = [prod(idx) for idx in zip ( * test_list)]
print ( "The Product of each index list is : " + str (res))
|
Output :
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Product of each index list is : [27, 63, 60]
Time complexity: O(n^2)
Auxiliary space: O(n)
Method #2: Using map() + loop + zip()
This works in almost similar way as the above method, but the difference is just that we use map function to build the product list rather than using list comprehension.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
print ( "The original list : " + str (test_list))
res = list ( map (prod, zip ( * test_list)))
print ( "The Product of each index list is : " + str (res))
|
Output :
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Product of each index list is : [27, 63, 60]
Time complexity: O(n^2), where n is the length of the inner lists.
Auxiliary space: O(n), where n is the length of the longest inner list.
Method #3 : Using functors.reduce() and operator.mul
Python3
test_list = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
print ( "The original list : " + str (test_list))
res = []
for i in range ( 0 , len (test_list)):
a = []
for j in range ( 0 , len (test_list[i])):
x = test_list[j][i]
a.append(x)
from functools import reduce
import operator
b = reduce (operator.mul, a, 1 )
res.append(b)
print ( "The Product of each index list is : " + str (res))
|
Output
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Product of each index list is : [27, 63, 60]
Time complexity: O(n^2), where n is the length of the input list
Auxiliary space: O(n), where n is the length of the input list.
Method #4: Using numpy.prod()
Note: Install numpy module using command “pip install numpy”
This method is recommended if the product of large array needs to be calculated. Here, numpy.prod() is used to get the product of each index.
Python3
import numpy as np
test_list = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
print ( "The original list : " + str (test_list))
res = [np.prod(idx) for idx in zip ( * test_list)]
print ( "The Product of each index list is : " + str (res))
|
Output
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Product of each index list is : [27, 63, 60]
Time Complexity: O(M*N)
Auxiliary Space: O(M*N)
Method #5: Using for loops
Python3
test_list = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
print ( "The original list : " + str (test_list))
res = []
for i in range ( 0 , len (test_list)):
p = 1
for j in range ( 0 , len (test_list[i])):
p * = test_list[j][i]
res.append(p)
print ( "The Product of each index list is : " + str (res))
|
Output
The original list : [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
The Product of each index list is : [27, 63, 60]
Time complexity: O(n^2)
Auxiliary space: O(n)
Method #6: Using the pandas library:
- Import the pandas library using the import pandas as pd statement.
- Define the input list of lists as list_of_lists = [[3, 7, 6], [1, 3, 5], [9, 3, 2]].
- Convert the list of lists to a pandas DataFrame using the pd.DataFrame() function and store it in the variable df.
- Compute the column products using the prod() method of the DataFrame df, which calculates the product of each column along the 0 axis (i.e., the columns), and store it in the variable column_products.
- Convert the resulting pandas Series to a regular Python list using the tolist() method and store it back in column_products.
- Print the column products using the print() statement, along with an appropriate message.
Python3
import pandas as pd
list_of_lists = [[ 3 , 7 , 6 ], [ 1 , 3 , 5 ], [ 9 , 3 , 2 ]]
df = pd.DataFrame(list_of_lists)
column_products = df.prod(axis = 0 ).tolist()
print ( "The product of each column is:" , column_products)
|
Output:
The product of each column is: [27, 63, 60]
Time complexity: O(NM), where N is the number of rows and M is the number of columns in the input list of lists.
Auxiliary space: O(NM), as the input list of lists is first converted to a pandas
Last Updated :
16 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...