Open In App

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 code to demonstrate
# Column Product in List of lists
# using loop + list comprehension + zip()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using loop + list comprehension + zip()
# Column Product in List of lists
res = [prod(idx) for idx in zip(*test_list)]
 
# print result
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 code to demonstrate
# Column Product in List of lists
# using map() + loop + zip()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + loop + zip()
# Column Product in List of lists
res = list(map(prod, zip(*test_list)))
 
# print result
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 code to demonstrate
# Column Product in List of lists
 
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Column Product in List of lists
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 result
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 code to demonstrate
# Column Product in List of lists
# using numpy.prod()
  
# importing numpy
import numpy as np
  
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using numpy.prod()
# Column Product in List of lists
res = [np.prod(idx) for idx in zip(*test_list)]
  
# print result
print("The Product of each index list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

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 code to demonstrate
# Column Product in List of lists
 
# initializing list
test_list = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Column Product in List of lists
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 result
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 pandas as pd
 
# Given input
list_of_lists = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
 
# Converting the list of lists to a DataFrame
df = pd.DataFrame(list_of_lists)
 
# Computing the column products using pandas
column_products = df.prod(axis=0).tolist()
 
# Printing the column products
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 


Article Tags :