Open In App

Python | Product of kth column in List of Lists

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop + zip() This task can be solved using the combination of above functions. In this, we pass in the zip() the list, to access all columns and explicit product function to get product of columns. 

Python3




# Python3 code to demonstrate working of
# Column Product in List of Lists
# using loop + zip()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
     
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize K
K = 2
 
# Column Product in List of Lists
# using loop + zip()
res = [prod(idx) for idx in zip(*test_list)][K]
 
# printing result
print("Product of Kth column is : " + str(res))


Output : 

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Kth column is : 56

Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.

Method #2 : Using loop This is brute force way to solve this problem. In this, we iterate through the matrix and take product of column by testing column number. 

Python3




# Python3 code to demonstrate working of
# Column Product in List of Lists
# Using loop
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize K
K = 2
 
# Column Product in List of Lists
# Using loop
res = 1
for sub in test_list:
    for idx in range(0, len(sub)):
        if idx == K:
            res = res * sub[idx]
 
# printing result
print("Product of Kth column is : " + str(res))


Output : 

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Kth column is : 56

Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.

Method #3 : Using numpy
Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
   
# printing original list
print("The original list is : ",test_list)
 
# initialize K
K = 2
   
# Column Product in List of Lists
# Using numpy
res = np.prod(np.array(test_list)[:, K])
 
# printing result
print("Product of Kth column is : ",res)
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list is :  [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Kth column is :  56

The time complexity of this approach is O(n) where n is the number of elements in the matrix. The auxiliary space is O(1) as we are not using any extra space.

Method 4 : use list comprehension along with the built-in function reduce() from the functools module.

Step-by-step approach:

  • Import the functools module.
  • Define the list of lists to be processed.
  • Set the value of K to the desired column index.
  • Use a list comprehension to extract the Kth element from each sublist in the main list and create a new list.
  • Use the reduce() function from the functools module to multiply all the elements in the new list together.
  • Print the result.

Python3




import functools
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
   
# printing original list
print("The original list is : ", test_list)
 
# initialize K
K = 2
 
# Using list comprehension and reduce()
res = functools.reduce(lambda x, y: x * y, [sublist[K] for sublist in test_list])
 
# printing result
print("Product of Kth column is : ", res)


Output

The original list is :  [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Kth column is :  56

The time complexity of this method is O(N), where N is the total number of elements in the list of lists.
The auxiliary space complexity is also O(N), as we create a new list of size N to store the Kth elements from each sublist.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads