# Python – Key Lists Summations

• Last Updated : 10 May, 2020

Sometimes, while working with Python Dictionaries, we can have problem in which we need to perform the replace of key with values with sum of all keys in values. This can have application in many domains that include data computations, such as Machine Learning. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using `sum()` + loop
This is one of the ways in which this task can be performed. In this, we perform the summation using sum, and iteration to each key is done in brute way using loop.

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

 `# Python3 code to demonstrate working of ``# Key Values Summations``# Using sum() + loop`` ` `# initializing dictionary``test_dict ``=` `{``'gfg'` `: [``4``, ``6``, ``8``], ``'is'` `: [``9``, ``8``, ``2``], ``'best'` `: [``10``, ``3``, ``2``]}`` ` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))`` ` `# Key Values Summations``# Using sum() + loop``for` `key, value ``in` `test_dict.items():``    ``test_dict[key] ``=` `sum``(value)``     ` `# printing result ``print``(``"The summation keys are : "` `+` `str``(test_dict)) `
Output :

The original dictionary is : {‘gfg’: [4, 6, 8], ‘is’: [9, 8, 2], ‘best’: [10, 3, 2]}
The summation keys are : {‘gfg’: 18, ‘is’: 19, ‘best’: 15}

Method #2 : Using dictionary comprehension + `sum()`
This is yet another way in which this task can be performed. This is similar to above method, just a shorthand version.

 `# Python3 code to demonstrate working of ``# Key Values Summations``# Using dictionary comprehension + sum()`` ` `# initializing dictionary``test_dict ``=` `{``'gfg'` `: [``4``, ``6``, ``8``], ``'is'` `: [``9``, ``8``, ``2``], ``'best'` `: [``10``, ``3``, ``2``]}`` ` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))`` ` `# Key Values Summations``# Using dictionary comprehension + sum()``res ``=` `{key : ``sum``(val) ``for` `key, val ``in` `test_dict.items()}``     ` `# printing result ``print``(``"The summation keys are : "` `+` `str``(res)) `
Output :

The original dictionary is : {‘gfg’: [4, 6, 8], ‘is’: [9, 8, 2], ‘best’: [10, 3, 2]}
The summation keys are : {‘gfg’: 18, ‘is’: 19, ‘best’: 15}

My Personal Notes arrow_drop_up