Python – Dictionary Values Mean
Last Updated :
05 Jun, 2023
Given a dictionary, find the mean of all the values present.
Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4}
Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean.
Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15}
Output : 10.0
Explanation : Mean of these is 10.0
Method #1 : Using loop + len()
This is a brute way in which this task can be performed. In this, we loop through each value and perform summation and then the result is divided by total keys extracted using len().
Python3
test_dict = { "Gfg" : 4 , "is" : 7 , "Best" : 8 , "for" : 6 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
res = 0
for val in test_dict.values():
res + = val
res = res / len (test_dict)
print ( "The computed mean : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10}
The computed mean : 7.0
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(1) constant additional space required
Method #2 : Using sum() + len() + values()
The combination of above functions can be used to solve this problem. In this, we perform summation using sum() and size() of total keys computed using len().
Python3
test_dict = { "Gfg" : 4 , "is" : 7 , "Best" : 8 , "for" : 6 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
res = sum (test_dict.values()) / len (test_dict)
print ( "The computed mean : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10}
The computed mean : 7.0
Method #3 : Using values() and mean() method of statistics module
Python3
import statistics
test_dict = { "Gfg" : 4 , "is" : 7 , "Best" : 8 , "for" : 6 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
res = statistics.mean( list (test_dict.values()))
print ( "The computed mean : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10}
The computed mean : 7
Method 4:Using the reduce function from the functools library
Using reduce function to calculate the sum of values and then dividing by the length of the dictionary
Approach:
- Import the reduce function from the functools library
- Define a function accumulate to take two arguments x and y, and return their sum (i.e., x+y)
- Use the reduce function to apply the accumulate function to all the values of the dictionary to get their sum
- Divide the sum by the length of the dictionary to get the mean
- Return the mean
Python3
from functools import reduce
def accumulate(x, y):
return x + y
def dict_mean(d):
values_sum = reduce (accumulate, d.values())
mean = values_sum / len (d)
return mean
d = { 'Gfg' : 4 , 'is' : 7 , 'Best' : 8 , 'for' : 6 , 'Geeks' : 10 }
print ( "Mean:" , dict_mean(d))
|
Time complexity: O(n)
Space complexity: O(1)
Method 5:using the NumPy module
Approach:
- Import the NumPy module.
- Convert the dictionary values to a NumPy array.
- Use the NumPy mean() function to compute the mean of the array.
- Print the computed mean.
Python3
import numpy as np
test_dict = { "Gfg" : 4 , "is" : 7 , "Best" : 8 ,
"for" : 6 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
res = np.mean( list (test_dict.values()))
print ( "The computed mean : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10}
The computed mean : 7.0
Time complexity:
Converting dictionary values to a NumPy array takes O(n) time, where n is the number of values in the dictionary.
Computing the mean using the NumPy mean() function takes O(1) time.
Therefore, the overall time complexity is O(n).
Auxiliary space complexity:
Converting dictionary values to a NumPy array requires O(n) auxiliary space.
Computing the mean using the NumPy mean() function requires O(1) auxiliary space.
Therefore, the overall auxiliary space complexity is O(n).
Please Login to comment...