 # How To Sum The Values In A List Of Dictionaries In Python How can you sum the values ​​in a list of dictionaries in Python? If you still have questions, we will introduce you some methods, such as list comprehension and map function,… Read the following article for more details.

## Sum the values ​​in a list of dictionaries in Python

### Use the sum, dict.values function and list comprehension

Example:

• Create a list of dictionaries.
• Use the dict.values() and list comprehension functions to find dictionary values.
• Convert those values ​​into a list and sum them using the sum function.
dictInfor = [
{"Id" : 111, "wages" : 100},
{"Id" : 112, "wages" : 200},
{"Id" : 113, "wages" : 300},
{"Id" : 114, "wages" : 100},
{"Id" : 115, "wages" : 200}
]

# Sum the values in list myInf
total = sum([sum(list(i.values())) for i in dictInfor])

print('Sum the values in the dictionary:', total)

Output:

Sum the values in the dictionary: 1465

Or you can use list comprehension as follows.

Example:

dictInfor = [
{"Id" : 111, "wages" : 100},
{"Id" : 112, "wages" : 200},
{"Id" : 113, "wages" : 300},
{"Id" : 114, "wages" : 100},
{"Id" : 115, "wages" : 200}
]

total1 = sum(item['Id'] for item in dictInfor)
total2 = sum(item['wages'] for item in dictInfor)

print('Sum the values in the dictionary:', total1 + total2)

Output:

Sum the values in the dictionary: 1465

### Use for loop

Example:

• Create a list of dictionaries.
• Use a nested for loop to find the dictionary’s values and sum them.
dictInfor = [
{"Id" : 111, "wages" : 100},
{"Id" : 112, "wages" : 200},
{"Id" : 113, "wages" : 300},
{"Id" : 114, "wages" : 100},
{"Id" : 115, "wages" : 200}
]

total = 0

# Use the for loop
for i in dictInfor:
for a in i:
total += i[a]

print('Sum the values in the dictionary:', total)

Output:

Sum the values in the dictionary: 1465

### Use the map function.

In this way, we will use the map function with the first parameter (The function to execute for each element in the iterable) being the operator.itemgetter function.

Syntax:

operator.itemgetter( item)

The operator.itemgetter function returns a callable object to fetch an item from its operand using the getitem method.

Example:

• Create a list of dictionaries.
• In the example above, the map() function will convert the value corresponding to the two keys ‘Id’ and ‘Wages’ of the dictionary into an object that can be called to fetch the item from its operand using the getitem method. (This happens because the first parameter of the map function is the operator.itemgetter function).
• Then perform the sum using the sum function.
import operator

dictInfor = [
{"Id" : 111, "wages" : 100},
{"Id" : 112, "wages" : 200},
{"Id" : 113, "wages" : 300},
{"Id" : 114, "wages" : 100},
{"Id" : 115, "wages" : 200}
]

totalId = sum(map(operator.itemgetter('Id'),dictInfor))
totalWages = sum(map(operator.itemgetter('wages'),dictInfor))

print('Sum the values in the dictionary:', totalId + totalWages)

Output:

Sum the values in the dictionary: 1465

## Summary

Those are three ways we use to sum the values in a list of dictionaries in Python. You can use any methods that work for you. Please comment below if you have any questions, and we will answer you. Thanks for reading!

Maybe you are interested: