Python | Product of kth column in List of Lists
Last Updated :
04 May, 2023
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
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [[ 5 , 6 , 7 ],
[ 9 , 10 , 2 ],
[ 10 , 3 , 4 ]]
print ("The original list is : " + str (test_list))
K = 2
res = [prod(idx) for idx in zip ( * test_list)][K]
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
test_list = [[ 5 , 6 , 7 ],
[ 9 , 10 , 2 ],
[ 10 , 3 , 4 ]]
print ("The original list is : " + str (test_list))
K = 2
res = 1
for sub in test_list:
for idx in range ( 0 , len (sub)):
if idx = = K:
res = res * sub[idx]
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
test_list = [[ 5 , 6 , 7 ],
[ 9 , 10 , 2 ],
[ 10 , 3 , 4 ]]
print ( "The original list is : " ,test_list)
K = 2
res = np.prod(np.array(test_list)[:, K])
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 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
test_list = [[ 5 , 6 , 7 ],
[ 9 , 10 , 2 ],
[ 10 , 3 , 4 ]]
print ( "The original list is : " , test_list)
K = 2
res = functools. reduce ( lambda x, y: x * y, [sublist[K] for sublist in test_list])
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
Share your thoughts in the comments
Please Login to comment...