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 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 loop to get product of columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Nth column Matrix Product 
# using loop + zip() 
  
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 N
N = 2
  
# Nth column Matrix Product 
# using loop + zip() 
res = [prod(idx) for idx in zip(*test_list)][N] 
  
# printing result 
print("Product of Nth column is : " + str(res)) 

chevron_right


Output :

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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Nth column Matrix Product  
# 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 N
N = 2
  
# Nth column Matrix Product  
# Using loop 
res = 1
for sub in test_list: 
    for idx in range(0, len(sub)): 
        if idx == N: 
            res = res * sub[idx] 
  
# printing result 
print("Product of Nth column is : " + str(res)) 

chevron_right


Output :

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.