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 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)) |
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 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)) |
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
Recommended Posts:
- Python | Summation of dictionary list values
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Summation of Non-Zero groups
- Python | Column summation of tuples
- Python | Summation of tuples in list
- Python | Strings length summation
- Python | Summation of two list of tuples
- Python | Summation of integers in heterogenous list
- Python | Grouped summation of tuple list
- Python - Get summation of numbers in string list
- Python | Summation of list as tuple attribute
- Python | Summation of Kth Column of Tuple List
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.


