Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – List of dictionaries all values Summation

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}, {“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))


Output

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


Output

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


Output

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments