Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Column Product in List of lists

  • Last Updated : 14 May, 2020

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# 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]

 

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]



My Personal Notes arrow_drop_up
Recommended Articles
Page :