Python – Value list Mean
Sometimes, while working with Python dictionary values, we can have problems in which we need to find mean of all the values of all dictionary value lists. This problem can have applications across many domains including web development. Let’s discuss certain ways in which this problem can be solved.
Input : test_dict = {'best': [11, 21], 'Gfg': [7, 5]} Output : {'best': 16.0, 'Gfg': 6.0}
Input : test_dict = {'Gfg' : [9]} Output : {'Gfg': 9.0}
Method #1: Using loop + sum() + len()
The combination of above functions can be used to solve this problem. In this, we compute the sum of all values using sum() and all value list lengths using len(). The iteration is done using loop.
Python3
# Python3 code to demonstrate working of # Value list mean # Using loop + sum() + len() # initializing dictionary test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Value list mean # Using loop + sum() + len() res = dict () for key in test_dict: res[key] = sum (test_dict[key]) / len (test_dict[key]) # printing result print ("The dictionary average is : " + str (res)) |
The original dictionary is : {‘is’: [10, 11, 2, 1], ‘best’: [12, 1, 2], ‘Gfg’: [6, 7, 5, 4]} The dictionary average is : {‘is’: 6.0, ‘best’: 5.0, ‘Gfg’: 5.5}
Method #2: Using dictionary comprehension
This is yet another way in which this task can be performed. This is shorthand, with help of which this task can be performed similar to the above method.
Python3
# Python3 code to demonstrate working of # Value list mean # Using dictionary comprehension # initializing dictionary test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Value list mean # Using dictionary comprehension res = {key: sum (val) / len (val) for key, val, in test_dict.items()} # printing result print ("The dictionary average is : " + str (res)) |
The original dictionary is : {‘is’: [10, 11, 2, 1], ‘best’: [12, 1, 2], ‘Gfg’: [6, 7, 5, 4]} The dictionary average is : {‘is’: 6.0, ‘best’: 5.0, ‘Gfg’: 5.5}
Method #3 : Using mean() method of statistics module
Python3
# Python3 code to demonstrate working of # Value list mean import statistics # initializing dictionary test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Value list mean res = dict () for key in test_dict: res[key] = float (statistics.mean(test_dict[key])) # printing result print ( "The dictionary average is : " + str (res)) |
The original dictionary is : {'Gfg': [6, 7, 5, 4], 'is': [10, 11, 2, 1], 'best': [12, 1, 2]} The dictionary average is : {'Gfg': 5.5, 'is': 6.0, 'best': 5.0}
Method#4: Using map(), lambda and dictionary comprehension.
The given code calculates the mean value of each list of values in the input dictionary and returns a new dictionary where each key is associated with the corresponding mean value.
Algorithm
1. Define a lambda function mean_func that takes a list lst as input and returns the mean value of the list.
2. Define a function mean_dict that takes an input dictionary test_dict as input.
3. Use the map() function to apply the mean_func function to each value list in the input dictionary.
4. Convert the map object to a tuple using the tuple() function to make it subscriptable.
5. Use the zip() function to combine the original keys from the input dictionary and the mean values calculated from the map() function.
6. Create a new dictionary using a dictionary comprehension that adds each key-value pair from the zip() result as a key-value pair in the new dictionary.
7. Return the new dictionary.
Python3
mean_func = lambda lst: sum (lst) / len (lst) def mean_dict(test_dict): mean_values = map (mean_func, test_dict.values()) return {key: value for key, value in zip (test_dict.keys(), tuple (mean_values))} test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]} print (mean_dict(test_dict)) |
{'Gfg': 5.5, 'is': 6.0, 'best': 5.0}
Time complexity: O(n), where n is the number of values/key-value pairs in the input dictionary.
Auxiliary Space: O(n), where n is the number of values/key-value pairs in the input dictionary.
METHOD 5:Using the statistics module.
APPROACH:
The given code computes the mean of the list of numbers [5, 10, 15, 20, 25] using the mean() function from the statistics module.
ALGORITHM:
1.Import the statistics module.
2.Initialize a list of numbers.
3.Use the mean() function from the statistics module to calculate the mean of the list.
4.Print the mean.
Python3
import statistics numbers = [ 5 , 10 , 15 , 20 , 25 ] mean = statistics.mean(numbers) print ( "Mean of the given list is:" , mean) |
Mean of the given list is: 15
Time complexity: O(n)
Auxiliary Space: O(n) (due to importing the statistics module)
METHOD 6:Using NumPy
Algorithm:
Step 1: Import the NumPy library.
Step 2: Define a dictionary with key-value pairs.
Step 3: Print the original dictionary.
Step 4: Use a dictionary comprehension and the NumPy mean() function to calculate the mean of the values for each key in the dictionary.
Step 5: Create a new dictionary with the same keys and the mean values.
Step 6: Print the new dictionary with the mean values.
Python3
# Python3 code to demonstrate working of # Value list mean # Using NumPy import numpy as np # initializing dictionary test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Value list mean # Using NumPy res = {key: np.mean(val) for key, val in test_dict.items()} # printing result print ( "The dictionary average is : " + str (res)) |
Output
The original dictionary is : {'Gfg': [6, 7, 5, 4], 'is': [10, 11, 2, 1], 'best': [12, 1, 2]} The dictionary average is : {'Gfg': 5.5, 'is': 6.0, 'best': 5.0}
Time Complexity:
The time complexity of this approach is O(n), where n is the number of key-value pairs in the dictionary. The NumPy mean() function has a time complexity of O(n), where n is the number of elements in the input array. In this case, the input array has the same length as the list of values for each key in the dictionary. Thus, the total time complexity of this approach is O(n) * O(n) = O(n^2). However, this can be simplified to O(n), since the length of the input arrays is constant.
Auxiliary Space Complexity:
The auxiliary space complexity of this approach is O(n), where n is the number of key-value pairs in the dictionary. We create a new dictionary with the same number of key-value pairs as the original dictionary. Additionally, there is some overhead for importing the NumPy library.