Open In App

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

## Python3

 `# 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 : {'gfg': [4, 5, 6], 'is': [1, 3, 4], 'best': [7, 8, 9]}
The summations of product : 364```

Time complexity: O(nm), where n is the number of keys in the dictionary and m is the length of the value lists associated with each key.
Auxiliary Space: O(m), where m is the length of the value lists associated with each key. This is because the helper function mul creates a new list of length m to store the product of the corresponding elements from each value list.

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

 `# 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 : {'gfg': [4, 5, 6], 'is': [1, 3, 4], 'best': [7, 8, 9]}
The summations of product : 364```

The time complexity of this code is O(N*M), where N is the number of keys in the dictionary and M is the length of the longest list in the dictionary.

The space complexity of this code is O(M), where M is the length of the longest list in the dictionary.

Method 3: Using list comprehension + sum()

Uses a list comprehension to calculate the product of the corresponding elements and then calculates the sum of the products using the built-in sum() function.

## Python3

 `# 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 list comprehension and sum()``res ``=` `sum``([a``*``b``*``c ``for` `a, b, c ``in` `zip``(``*``test_dict.values())])` `# printing result``print``(``"The summation of products : "` `+` `str``(res))`

Output

```The original dictionary : {'gfg': [4, 5, 6], 'is': [1, 3, 4], 'best': [7, 8, 9]}
The summation of products : 364```

Time complexity: O(n), where n is the length of the dictionary values.
Auxiliary space: O(n) because both create an iterable object (either a list comprehension or a zip object) that contains n elements.