Sum the values in a list of dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Jul 3, 2022

banner

Photo from Unsplash

Sum the values in a list of dictionaries in Python #

To sum the values in a list of dictionaries:

  1. Use a generator expression to iterate over the list.
  2. On each iteration, access the current dictionary at the specific key.
  3. Pass the generator expression to the sum() function.
main.py
from collections import Counter # ✅ sum values in list of dictionaries for specific dict key list_of_dicts = [ {'name': 'Alice', 'salary': 100}, {'name': 'Bob', 'salary': 100}, {'name': 'Carl', 'salary': 100}, ] total = sum(d['salary'] for d in list_of_dicts) print(total) # 👉️ 300 # --------------------------------------------- # ✅ sum values in list of dictionaries for all dict keys list_of_dicts_2 = [ {'id': 1, 'salary': 100}, {'id': 2, 'salary': 100}, {'id': 3, 'salary': 100}, ] my_dict = Counter() for d in list_of_dicts_2: for key, value in d.items(): my_dict[key] += value # 👇️ Counter({'salary': 300, 'id': 6}) print(my_dict) total = sum(my_dict.values()) print(total) # 👉️ 306

We used a generator expression to iterate over the list of dictionaries.

main.py
list_of_dicts = [ {'name': 'Alice', 'salary': 100}, {'name': 'Bob', 'salary': 100}, {'name': 'Carl', 'salary': 100}, ] total = sum(d['salary'] for d in list_of_dicts) print(total) # 👉️ 300
Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we access the specific dict key to get the corresponding value and return the result.

The sum function takes an iterable, sums its items from left to right and returns the total.

The sum function takes the following 2 arguments:

NameDescription
iterablethe iterable whose items to sum
startsums the start value and the items of the iterable. sum defaults to 0 (optional)

If you need to sum the values in a list of dictionaries for all dictionary keys, use the Counter class.

main.py
from collections import Counter list_of_dicts_2 = [ {'id': 1, 'salary': 100}, {'id': 2, 'salary': 100}, {'id': 3, 'salary': 100}, ] my_dict = Counter() for d in list_of_dicts_2: for key, value in d.items(): my_dict[key] += value # 👇️ Counter({'salary': 300, 'id': 6}) print(my_dict) total = sum(my_dict.values()) print(total) # 👉️ 306

The Counter class from the collections module is a subclass of the dict class.

The class is a basically mapping of key-count pairs.

The values are allowed to be any integer (including zero and negative numbers).

The outer for loop in the example iterates over the list of dictionaries.

The inner loop iterates over the items of the current dictionary.

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
list_of_dicts_2 = [ {'id': 1, 'salary': 100}, {'id': 2, 'salary': 100}, {'id': 3, 'salary': 100}, ] # 👇️ dict_items([('id', 1), ('salary', 100)]) print(list_of_dicts_2[0].items())

On each iteration of one of the nested dictionaries, we update the key-count pair in the central Counter object.

The result is the sum of the values of all dictionary keys in the list of dictionaries.

Here is the complete code snippet.

main.py
from collections import Counter list_of_dicts_2 = [ {'id': 1, 'salary': 100}, {'id': 2, 'salary': 100}, {'id': 3, 'salary': 100}, ] my_dict = Counter() for d in list_of_dicts_2: for key, value in d.items(): my_dict[key] += value # 👇️ Counter({'salary': 300, 'id': 6}) print(my_dict) total = sum(my_dict.values()) print(total) # 👉️ 306
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.