Open In App

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

Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

Time complexity: O(N), where N is the total number of elements in all the lists contained within the dictionary.
Auxiliary space: O(1), as it only uses a constant amount of auxiliary space to store the input dictionary and the result variable.



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

Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

Method #3: Using values(), extend() and len() methods




# Python3 code to demonstrate working of
# Summation of dictionary list values
 
# 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
x=list(test_dict.values())
a=[]
for i in x:
    a.extend(i)
res=len(a)
     
# printing result
print("Summation of dictionary list values are : " + str(res))

Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

Method #4: Using reduce()

Using reduce() and len(): This approach uses the reduce() function from the functools module to iteratively combine the values of the dictionary into a single iterable, and then uses the len() function to get the count of elements in the iterable. Time complexity is O(n) where n is the total number of elements in the lists, and auxiliary space is O(n) where n is the total number of elements in the lists.




from functools import reduce
 
# 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 reduce() and len()
# reduce() method applies a given function to all elements in an iterable and returns a single value
# len() method returns the length of the passed iterable
res = len(reduce(lambda x, y: x + y, test_dict.values()))
 
# printing result
print("Summation of dictionary list values are : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy

Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

The time complexity of this code is O(N*M), where N is the number of keys in the dictionary and M is the length of the longest list in the dictionary.

The space complexity of this code is O(M), where M is the length of the longest list in the dictionary.

Method#5 Using Numpy library




import numpy as np
 
# 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))
 
# convert dictionary values to numpy array
values_array = np.array(list(test_dict.values()))
 
# flatten array to 1D
values_array = values_array.flatten()
 
# sum the values in the array
result = np.sum(values_array)
 
print(len(result))
#This code is contributed by Vinay Pinjala.

Output:

The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

Time complexity: O(n)
Auxiliary Space: O(n)

Method#6: Using a for loop:




def sum_dict_values(test_dict):
    # Initialize a variable to store the result
    result = 0
    # Iterate through all the values of the dictionary
    for sub_list in test_dict.values():
        # Add the length of each sub-list to the result
        result += len(sub_list)
    # Return the result
    return result
# Test the function with a sample 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))
result = sum_dict_values(test_dict)
print("Summation of dictionary list values are : " + str(result))
#This is code is contributed by Jyothi pinjala

Output
The original dictionary is : {'gfg': [5, 6, 7], 'is': [10, 11], 'best': [19, 31, 22]}
Summation of dictionary list values are : 8

Time complexity: O(n)
Auxiliary Space: O(1)


Article Tags :