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.
Similar Reads
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. Meth
6 min read
Python - Kth Column Product in Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the product of itâs Kth index. This problem has application in web development domain while working with data informations. Letâs discuss certain ways in which this task can be performed. M
7 min read
Python - Product of i^k in List
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of i^k of list in just one line. Letâs discuss
5 min read
Python | Product of Prefix in list
Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Letâs discuss certain ways in which this problem can be solved. Method 1: Using list comprehen
4 min read
Iterate Over a List of Lists in Python
We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Python | Nth column Matrix Product
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
7 min read
Python - Cubes Product in list
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of cubes of list in just one line. Letâs discu
5 min read
Python - Product of consecutive pairs in list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be solved.
7 min read
Flatten a List of Lists in Python
Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python. Using itertools.chain itertools modu
3 min read
Python - Product of elements using Index list
Accessing an element from its index is easier task in python, just using the [] operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices and then perform the multipl
7 min read