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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Value summation of key in dictionary
# Using sum() + list comprehension
  
# Initialize list
test_list = [{'gfg' : 1, 'is' : 2, 'best' : 3},
             {'gfg' : 7, 'is' : 3, 'best' : 5},
             {'gfg' : 9, 'is' : 8, 'best' : 6}] 
  
# printing original list
print("The original list is : " + str(test_list))
  
# Value summation of key in dictionary
# Using sum() + list comprehension
res = sum(sub['gfg'] for sub in test_list)
  
# printing result
print("The sum of particular key is : " + str(res))

chevron_right


Output :

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Value summation of key in dictionary
# Using sum() + itemgetter() + map()
import operator
  
# Initialize list
test_list = [{'gfg' : 1, 'is' : 2, 'best' : 3},
             {'gfg' : 7, 'is' : 3, 'best' : 5},
             {'gfg' : 9, 'is' : 8, 'best' : 6}] 
  
# printing original list
print("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 result
print("The sum of particular key is : " + str(res))

chevron_right


Output :

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



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.