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
def prod(val):
res = 1
for ele in val:
res * = ele
return res
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = prod([ele for sub in test_list for ele in sub])
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
from itertools import chain
def prod(val):
res = 1
for ele in val:
res * = ele
return res
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = prod( list (chain( * test_list)))
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
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
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 ( "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
import operator
from functools import reduce
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.extend(i)
res = reduce (operator.mul, x, 1 )
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
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = 1
for i in test_list:
for j in i:
res * = j
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
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
test_list = [[ 1 , 4 , 5 ], [ 7 , 3 ], [ 4 ], [ 46 , 7 , 3 ]]
print ( "The original list : " + str (test_list))
res = multiply_nested_list(test_list)
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Apr, 2023
Like Article
Save Article