Python | Value summation of key in dictionary
Many operations such as grouping and conversions are possible using Python dictionaries. But sometimes, we can also have a problem in which we need to perform the aggregation of values of key in dictionary list. This task is common in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using sum() + list comprehension
This is the one-liner approach to perform the task of getting the sum of particular key while iterating to the similar keys in list of dictionaries using list comprehension.
# Python3 code to demonstrate working of# Value summation of key in dictionary# Using sum() + list comprehension # Initialize listtest_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}] # printing original listprint("The original list is : " + str(test_list)) # Value summation of key in dictionary# Using sum() + list comprehensionres = sum(sub['gfg'] for sub in test_list) # printing resultprint("The sum of particular key is : " + str(res)) |
The original list is : [{‘best’: 3, ‘gfg’: 1, ‘is’: 2}, {‘best’: 5, ‘gfg’: 7, ‘is’: 3}, {‘best’: 6, ‘gfg’: 9, ‘is’: 8}]
The sum of particular key is : 17
Method #2 : Using sum() + itemgetter() + map()
The combination of these functions can also be used to perform this task. In this, the main difference is that the comprehension task is done by map() and the key access task is done by the itemgetter().
# Python3 code to demonstrate working of# Value summation of key in dictionary# Using sum() + itemgetter() + map()import operator # Initialize listtest_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}] # printing original listprint("The original list is : " + str(test_list)) # Value summation of key in dictionary# Using sum() + itemgetter() + map()res = sum(map(operator.itemgetter('gfg'), test_list)) # printing resultprint("The sum of particular key is : " + str(res)) |
The original list is : [{‘best’: 3, ‘gfg’: 1, ‘is’: 2}, {‘best’: 5, ‘gfg’: 7, ‘is’: 3}, {‘best’: 6, ‘gfg’: 9, ‘is’: 8}]
The sum of particular key is : 17


