Python - List of dictionaries all values Summation
Last Updated :
21 Apr, 2023
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}, {"Apple" : 2, "Mango" : 3, "Grapes" : 3}]
Output : 15
Explanation : Summation of all values leads to 15.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we iterate for all the dictionaries from list and then perform sum of all the keys of each dictionary.
Python3
# Python3 code to demonstrate working of
# List of dictionaries all values Summation
# Using loop
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10},
{"Gfg" : 8, "is" : 11, "best" : 19},
{"Gfg" : 2, "is" : 16, "best" : 10},
{"Gfg" : 12, "is" : 1, "best" : 8},
{"Gfg" : 22, "is" : 6, "best" : 8}]
# printing original list
print("The original list : " + str(test_list))
res = 0
# loop for dictionaries
for sub in test_list:
for key in sub:
# summation of each key
res += sub[key]
# printing result
print("The computed sum : " + str(res))
OutputThe original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The computed sum : 148
Time complexity: O(n), where n is the number of dictionaries in the list. This is because the code loops through each dictionary in the list, and for each dictionary it loops through each key, so the total number of iterations would be n * number of keys in a dictionary.
Auxiliary space: O(1), as the code only uses a single variable "res" to store the sum, and the space used by this variable does not grow with the size of the input.
Method #2 : Using sum() + values() + list comprehension
The combination of above functions can be used as one-liner alternative to solve this problem. In this, summation is performed using sum(), and values( ) is used to extract values of all dictionaries in list.
Python3
# Python3 code to demonstrate working of
# List of dictionaries all values Summation
# Using sum() + values() + list comprehension
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10},
{"Gfg" : 8, "is" : 11, "best" : 19},
{"Gfg" : 2, "is" : 16, "best" : 10},
{"Gfg" : 12, "is" : 1, "best" : 8},
{"Gfg" : 22, "is" : 6, "best" : 8}]
# printing original list
print("The original list : " + str(test_list))
res = sum([sum(list(sub.values())) for sub in test_list])
# printing result
print("The computed sum : " + str(res))
OutputThe original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The computed sum : 148
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Using the built-in function reduce() from the functools module.
Step-by-step approach :
- We first import the reduce() function from the functools module.
- We initialize the test_list list with the same values as before.
- We print the original list using print().
- We use reduce() function along with lambda function to sum up all the values of each dictionary present in test_list.
- The reduce() function takes three arguments - the lambda function, the test_list list, and the initial value of the sum, which is set to 0 in this case.
- The lambda function takes two arguments, x and y, where x represents the running sum and y represents each dictionary in test_list.
- The lambda function sums up the values of each dictionary using sum() and returns the updated value of the running sum x + sum(y.values()).
- The reduce() function performs this operation for each dictionary in test_list and returns the final sum of all the values in all the dictionaries.
- We print the result using print().
Python3
from functools import reduce
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10},
{"Gfg" : 8, "is" : 11, "best" : 19},
{"Gfg" : 2, "is" : 16, "best" : 10},
{"Gfg" : 12, "is" : 1, "best" : 8},
{"Gfg" : 22, "is" : 6, "best" : 8}]
# printing original list
print("The original list : " + str(test_list))
# using reduce() + values() + sum() to compute sum of all values
res = reduce(lambda x, y: x + sum(y.values()), test_list, 0)
# printing result
print("The computed sum : " + str(res))
OutputThe original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The computed sum : 148
Time Complexity: O(n), where n is the number of dictionaries in test_list.
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 - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 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 | 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 tas
6 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