Open In App

Python | Matrix Product

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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




# 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




# 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




# 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. 

  • Define a function multiply_nested_list that takes a nested list as an argument.
  • Initialize a variable res with the value 1.
  • Iterate over each element i in the input list using a for loop.
  • Check if the current element i is a list or not, using the isinstance() function.
  • If the current element i is a list, call the multiply_nested_list() function recursively with i as an argument, and multiply the result with the res variable.
  • If the current element i is not a list, multiply the res variable with i.
  • Return the final result stored in the res variable.
  • Initialize a test list test_list with nested elements.
  • Print the original test list.
  • Call the multiply_nested_list() function with the test_list as an argument and store the result in a variable res.
  • Print the total element product in lists by converting the res variable to a string and concatenating it with a message.

Python3




# 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.



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

Similar Reads