Python – Cumulative product of dictionary value lists
Last Updated :
13 Apr, 2023
Sometimes, while working with Python dictionaries, we can have it’s values as lists. In this can we can have a problem that we just require the product of elements in those list as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let’s discuss certain ways in which this task can be performed
Method #1 : Using loop + list comprehension
This task can be performed using explicit product function which can be used to get the product and internal list comprehension can provide a mechanism to iterate this logic to all the keys of dictionary.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_dict = { 'gfg' : [ 5 , 6 , 7 ], 'is' : [ 10 , 11 ], 'best' : [ 19 , 31 , 22 ]}
print ( "The original dictionary is : " + str (test_dict))
res = sum (prod(sub) for sub in test_dict.values())
print ( "Product of dictionary list values are : " + str (res))
|
Output :
The original dictionary is : {'best': [19, 31, 22], 'gfg': [5, 6, 7], 'is': [10, 11]}
Product of dictionary list values are : 13278
Time Complexity: O(n*n) where n is the number of elements in the string list. The loop + list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using loop + map()
This task can also be performed using map function in place of list comprehension to extend the logic of finding the product, rest all the functionality remaining same as the above method.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_dict = { 'gfg' : [ 5 , 6 , 7 ], 'is' : [ 10 , 11 ], 'best' : [ 19 , 31 , 22 ]}
print ( "The original dictionary is : " + str (test_dict))
res = sum ( map (prod, test_dict.values()))
print ( "Product of dictionary list values are : " + str (res))
|
Output :
The original dictionary is : {'best': [19, 31, 22], 'gfg': [5, 6, 7], 'is': [10, 11]}
Product of dictionary list values are : 13278
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant space required
Method #3 : Using reduce and lambda:
Python3
from functools import reduce
test_dict = { 'gfg' : [ 5 , 6 , 7 ], 'is' : [ 10 , 11 ], 'best' : [ 19 , 31 , 22 ]}
print ( "The original dictionary is : " + str (test_dict))
res = sum ( reduce ( lambda x, y: x * y, sub) for sub in test_dict.values())
print ( "Product of dictionary list values are : " + str (res))
|
Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Product of dictionary list values are : 13278
In this code, we use lambda function instead of prod function which takes two argument x, y and return x * y. The reduce function is used to apply the lambda function to all elements of the lists, and the sum function is used to add up the results.
Time Complexity: O(n), where n is the number of elements in all lists of the dictionary, since the map and sum functions iterate through all elements of the lists.
Auxiliary Space: O(1), since only a few variables are used in the process.
Share your thoughts in the comments
Please Login to comment...