Open In App

Python | Matrix Product

Getting the product of list is quite common problem and has been dealt with and discussed many times, but sometimes, we require to better it and total product, i.e. including those of nested list as well. Let’s try and get the total product and solve this particular problem. 

Method #1: Using list comprehension + loop 



We can solve this problem using the list comprehension as a potential shorthand to the conventional loops that we may use to perform this particular task. We just iterate and product the nested list and at end return the cumulative product using function. 




# Python3 code to demonstrate
# Matrix Product
# Using list comprehension + loop
 
# getting Product
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + loop
# Matrix Product
res = prod([ele for sub in test_list for ele in sub])
 
# print result
print("The total element product in lists is : " + str(res))

Output

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(N*M)
Auxiliary Space: O(1)

Method #2: Using chain() + loop 

This particular problem can also be solved using the chain function instead of list comprehension in which we use the conventional function to perform product. 




# Python3 code to demonstrate
# Matrix Product
# Using chain() + loop
from itertools import chain
 
# getting Product
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using chain() + loop
# Matrix Product
res = prod(list(chain(*test_list)))
 
# print result
print("The total element product in lists is : " + str(res))

Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(N)
Auxiliary Space: O(1)

Method #3 : Using extend() method




# Python3 code to demonstrate
# Matrix Product
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
x = []
for i in test_list:
    x.extend(i)
res = 1
for j in x:
    res *= j
 
 
# print result
print("The total element product in lists is : " + str(res))

Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #4 : Using extend(), functools.reduce() and operator.mul




# Python3 code to demonstrate
# Matrix Product
 
# initializing list
import operator
from functools import reduce
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
x = []
for i in test_list:
    x.extend(i)
 
res = reduce(operator.mul, x, 1)
 
# print result
print("The total element product in lists is : " + str(res))

Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #5:Using Nested Loops

This is the naive approach to solve the problem




# Python3 code to demonstrate
# Matrix Product
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
res = 1
for i in test_list:
    for j in i:
        res *= j
 
# print result
print("The total element product in lists is : " + str(res))

Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(N*M)
Auxiliary Space: O(1), Where N is the number of rows and M is the number of columns

Method 6 :  use recursion to traverse the nested list and multiply all the elements. 




# function to calculate the product of all elements in the nested list
def multiply_nested_list(nested_list):
    res = 1
    for i in nested_list:
        if isinstance(i, list):
            res *= multiply_nested_list(i)
        else:
            res *= i
    return res
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
# call the function to get the product
res = multiply_nested_list(test_list)
 
# print result
print("The total element product in lists is : " + str(res))

Output
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element product in lists is : 1622880

Time Complexity: O(n) where n is the number of elements in the nested list.
Auxiliary Space: O(d) where d is the depth of the nested list.


Article Tags :