Sometimes, while working with Python dictionaries, we can have it’s values as lists. In this can we can have a problem that we just require the count of elements in those list as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using sum() + list comprehension
This task can be performed using sum function which can be used to get the summation and internal list comprehension can provide a mechanism to iterate this logic to all the keys of dictionary.
# Python3 code to demonstrate working of # Summation of dictionary list values # using sum() + list comprehension # initialize dictionary test_dict = {'gfg' : [5, 6, 7], 'is' : [10, 11], 'best' : [19, 31, 22]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Summation of dictionary list values # using sum() + list comprehension res = sum(len(sub) for sub in test_dict.values()) # printing result print("Summation of dictionary list values are : " + str(res)) |
The original dictionary is : {'best': [19, 31, 22], 'is': [10, 11], 'gfg': [5, 6, 7]}
Summation of dictionary list values are : 8
Method #2 : Using sum() + map()
This task can also be performed using map function in place of list comprehension to extend the logic of finding the length, rest all the functionality remaining same as the above method.
# Python3 code to demonstrate working of # Summation of dictionary list values # using sum() + map() # initialize dictionary test_dict = {'gfg' : [5, 6, 7], 'is' : [10, 11], 'best' : [19, 31, 22]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Summation of dictionary list values # using sum() + map() res = sum(map(len, test_dict.values())) # printing result print("Summation of dictionary list values are : " + str(res)) |
The original dictionary is : {'best': [19, 31, 22], 'is': [10, 11], 'gfg': [5, 6, 7]}
Summation of dictionary list values are : 8
Recommended Posts:
- Python - Summation of tuple dictionary values
- Python - Dictionary values String Length Summation
- Python - Nested Dictionary values summation
- Python - Product and Inter Summation dictionary values
- Python - Sort Dictionary by Values Summation
- Python - Dictionary Keys whose Values summation equals K
- Python - Summation Grouping in Dictionary List
- Python - List of dictionaries all values Summation
- Python - Convert Dictionary Value list to Dictionary List
- Python - Filter dictionary values in heterogenous dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Extract Dictionary values list to List
- Python - Assigning Key values to list elements from Value list Dictionary
- Python | Value summation of key in dictionary
- Python - Tuple to Dictionary Summation conversion
- Python - Custom Tuple Key Summation in Dictionary
- Python - Summation of Custom nested keys in Dictionary
- Python - Extract Dictionary Items with Summation Greater than K
- Python - Extract Unique values dictionary values
- Python - Remove duplicate values across Dictionary Values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

