Python | Value summation of key in dictionary
Last Updated :
13 Apr, 2023
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
# 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))
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
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
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
# 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))
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
Time Complexity: O(n) where n is the number of elements in the test_list. The sum() + itemgetter() + map() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is required.
Method #3: Using reduce() + lambda function
This is another way to perform the summation of key values in a list of dictionaries. In this method, we use the reduce() function to iterate through the list of dictionaries and the lambda function to perform the summation of specific key values.
Python3
# Python3 code to demonstrate working of
# Value summation of key in dictionary
# Using reduce() + lambda function
from functools import reduce
# 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 reduce() + lambda function
res = reduce(lambda x, y: x + y['gfg'], test_list, 0)
# printing result
print("The sum of particular key is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 7, 'is': 3, 'best': 5}, {'gfg': 9, 'is': 8, 'best': 6}]
The sum of particular key is : 17
Time complexity: O(n)
Auxiliary Space: O(1)
Method #4: Using a for loop
Use a for loop to iterate over the list of dictionaries and add up the values of the key 'gfg'.
Python3
# Python3 code to demonstrate working of
# Value summation of key in dictionary
# Using for loop
# 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 for loop
res = 0
for d in test_list:
res += d['gfg']
# printing result
print("The sum of particular key is : " + str(res))
OutputThe original list is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 7, 'is': 3, 'best': 5}, {'gfg': 9, 'is': 8, 'best': 6}]
The sum of particular key is : 17
Time complexity: O(n), where n is the length of the list. This is because we are iterating over each dictionary in the list once, and accessing a single key from each dictionary.
Auxiliary space: O(1), since we are only using a single variable to keep track of the sum and not using any extra data structures.
Method 5: Using Generator Expression and sum()
STEP:
- Create a generator expression that yields the value of key 'gfg' from each dictionary in test_list.
- Use sum() function to calculate the sum of all the values yielded by the generator expression.
- Print the final result.
Python3
# Python3 code to demonstrate working of
# Value summation of key in dictionary
# Using Generator Expression and sum()
# 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 Generator Expression and sum()
res = sum(d['gfg'] for d in test_list)
# printing result
print("The sum of particular key is : " + str(res))
OutputThe original list is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 7, 'is': 3, 'best': 5}, {'gfg': 9, 'is': 8, 'best': 6}]
The sum of particular key is : 17
Time complexity: O(n)
Auxiliary space: O(1)
Similar Reads
Python | Summation of dictionary list values
Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
6 min read
Python - Nested Dictionary values summation
Sometimes, while working with Python dictionaries, we can have problem in which we have nested records and we need cumulative summation of it's keys values. This can have possible application in domains such as web development and competitive programming. Lets discuss certain ways in which this task
8 min read
Python - Sort Dictionary by key-value Summation
Given a Dictionary, sort by summation of key and value. Input : test_dict = {3:5, 1:3, 4:6, 2:7, 8:1} Output : {1: 3, 3: 5, 2: 7, 8: 1, 4: 6} Explanation : 4 < 8 < 9 = 9 < 10 are increasing summation of keys and values. Input : test_dict = {3:5, 1:3, 4:6, 2:7} Output : {1: 3, 3: 5, 2: 7, 4:
5 min read
Python - Summation of tuple dictionary values
Sometimes, while working with data, we can have a problem in which we need to find the summation of tuple elements that are received as values of dictionary. We may have a problem to get index wise summation. Letâs discuss certain ways in which this particular problem can be solved. Method #1: Using
4 min read
Python - Dictionary Values Mapped Summation
Given a dictionary with a values list, our task is to extract the sum of values, found using mappings. Input : test_dict = {4 : ['a', 'v', 'b', 'e'], 1 : ['g', 'f', 'g'], 3 : ['e', 'v']}, map_vals = {'a' : 3, 'g' : 8, 'f' : 10, 'b' : 4, 'e' : 7, 'v' : 2} Output : {4: 16, 1: 26, 3: 9} Explanation : "
6 min read
Python - Custom Tuple Key Summation in Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform group summation of values, of certain key on particular index of tuple keys of dictionary. This problem is quite custom, but can have application in domains that revolves around data processing. Let'
7 min read
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 tas
4 min read
Python - List of dictionaries all values Summation
Given a list of dictionaries, extract all the values summation. Input : test_list = [{"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}] Output : 12 Explanation : 2 + 2 +...(6-times) = 12, sum of all values. Input : test_list = [{"Apple" : 3, "Mango" : 2, "Grapes" : 2
5 min read
Python - Summation of Custom nested keys in Dictionary
Given a dictionary with keys as nested dictionaries, find the sum of values of certain custom keys inside the nested dictionary. Input : test_dict = {'Gfg' : {1 : 6, 5: 9, 9: 12}, 'is' : {1 : 9, 5: 7, 9: 2}, 'best' : {1 : 3, 5: 4, 9: 14}}, sum_key = [1] Output : 18 Explanation : 6 + 9 + 3 = 18, only
10 min read
Python - Dictionary Keys whose Values summation equals K
Given a dictionary and a value K, extract keys whose summation of values equals K. Input : {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}, K = 17 Output : ['Best', 'for'] Explanation : 9 + 8 = 17, hence those keys are extracted. Input : {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks
9 min read