Related Articles
Python – Product and Inter Summation dictionary values
• Last Updated : 10 May, 2020

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.

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

 `# 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)) `
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up