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
# Python3 code to demonstrate working of
# Cumulative product of dictionary value lists
# using loop + list comprehension
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initialize dictionary
test_dict = {'gfg' : [5, 6, 7], 'is' : [10, 11], 'best' : [19, 31, 22]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Cumulative product of dictionary value lists
# using loop + list comprehension
res = sum(prod(sub) for sub in test_dict.values())
# printing result
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
# Python3 code to demonstrate working of
# Cumulative product of dictionary value lists
# using loop + map()
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initialize dictionary
test_dict = {'gfg' : [5, 6, 7], 'is' : [10, 11], 'best' : [19, 31, 22]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Cumulative product of dictionary value lists
# using loop + map()
res = sum(map(prod, test_dict.values()))
# printing result
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
# initialize dictionary
test_dict = {'gfg' : [5, 6, 7], 'is' : [10, 11], 'best' : [19, 31, 22]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Cumulative product of dictionary value lists
# using reduce function and lambda
res = sum(reduce(lambda x, y: x * y, sub) for sub in test_dict.values())
# printing result
print("Product of dictionary list values are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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.
Similar Reads
Calculating the Product of List Lengths in a Dictionary - Python The task of calculating the product of the lengths of lists in a dictionary involves iterating over the dictionaryâs values, which are lists and determining the length of each list. These lengths are then multiplied together to get a single result. For example, if d = {'A': [1, 2, 3], 'B': [4, 5], '
3 min read
Python | Concatenate dictionary value lists Sometimes, while working with dictionaries, we might have a problem in which we have lists as it's value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Usi
5 min read
Python - Tuple value product in dictionary Sometimes, while working with data, we can have a problem in which we need to find the product of tuple elements that are received as values of dictionary. We may have a problem to get index wise product. Letâs discuss certain ways in which this particular problem can be solved. Method #1 : Using tu
5 min read
Python - Dictionary Key Value lists combinations Given a dictionary with values as a list, extract all the possible combinations, both cross keys and with values. Input : test_dict = {"Gfg" : [4, 5], "is" : [1, 2], "Best" : [9, 4]} Output : {0: [['Gfg', 4], ['is', 1], ['Best', 9]], 1: [['Gfg', 4], ['is', 1], ['Best', 4]], 2: [['Gfg', 4], ['is', 2]
9 min read
Python | Combining values from dictionary of list Given a dictionary of list values, the task is to combine every key-value pair in every combination. Input : {"Name" : ["Paras", "Chunky"], "Site" : ["Geeksforgeeks", "Cyware", "Google"] } Output: [{'Site': 'Geeksforgeeks', 'Name': 'Paras'}, {'Site': 'Cyware', 'Name': 'Paras'}, {'Site': 'Google', 'N
2 min read
Python | Combining values from dictionary of list Given a dictionary of list values, the task is to combine every key-value pair in every combination. Input : {"Name" : ["Paras", "Chunky"], "Site" : ["Geeksforgeeks", "Cyware", "Google"] } Output: [{'Site': 'Geeksforgeeks', 'Name': 'Paras'}, {'Site': 'Cyware', 'Name': 'Paras'}, {'Site': 'Google', 'N
2 min read