Python – Product and Inter Summation dictionary values

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform product of entire value list and perform summation of product of each list with other. This kind of application in web development and day-day programming. Lets discuss certain ways in which this task can be performed.

Input : test_dict = {‘gfg’ : [4], ‘is’ : [3], ‘best’ : [5]}
Output : 60

Input : test_dict = {‘gfg’ : [0]}
Output : 0

Method #1 : Using zip() + map() + sum() + loop
The combination of above functions can be used to solve this problem. In this, we perform the summation of values using sum(), the zip() binds all the values. The map() is used to bind multiplication logic in all elements in value list. All this is bound using loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Product and Inter Summation dictionary values
# Using zip() + map() + sum() + loop 
  
# helper function
def mul(sub):
  res = 1
  for ele in sub:
     res *= int(ele)
  return res
  
# initializing dictionary
test_dict = {'gfg' : [4, 5, 6], 'is' : [1, 3, 4], 'best' : [7, 8, 9]}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Product and Inter Summation dictionary values
# Using zip() + map() + sum() + loop 
temp = zip(*test_dict.values())
res = sum(map(mul, temp))  
  
# printing result 
print("The summations of product : " + str(res)) 

chevron_right


Output :



The original dictionary : {‘best’: [7, 8, 9], ‘is’: [1, 3, 4], ‘gfg’: [4, 5, 6]}
The summations of product : 364

 

Method #2 : Using map() + reduce() + lambda + zip() + sum() + generator expression
The combination of above functionalities can be used to solve this problem. In this, we perform the task of multiplication using reduce and lambda and generator expression performs the task of iteration.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Product and Inter Summation dictionary values
# Using map() + reduce() + lambda + zip() + sum() + generator expression
from functools import reduce
  
# initializing dictionary
test_dict = {'gfg' : [4, 5, 6], 'is' : [1, 3, 4], 'best' : [7, 8, 9]}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Product and Inter Summation dictionary values
# Using map() + reduce() + lambda + zip() + sum() + generator expression
res = sum(map(lambda ele: reduce(lambda x, y: int(x) * int(y), ele),
                                          zip(*test_dict.values())))
  
# printing result 
print("The summations of product : " + str(res)) 

chevron_right


Output :

The original dictionary : {‘best’: [7, 8, 9], ‘is’: [1, 3, 4], ‘gfg’: [4, 5, 6]}
The summations of product : 364

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.