Open In App

Python | Value summation of key in dictionary

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

The 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))


Output

The 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: 

  1. Create a generator expression that yields the value of key ‘gfg’ from each dictionary in test_list.
  2. Use sum() function to calculate the sum of all the values yielded by the generator expression.
  3. 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))


Output

The 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads