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
test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]}
print ("The original dictionary is : " + str (test_dict))
res = dict ()
for key in test_dict:
res[key] = sum (test_dict[key]) / len (test_dict[key])
print ("The dictionary average is : " + str (res))
|
Output :
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
test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]}
print ("The original dictionary is : " + str (test_dict))
res = {key: sum (val) / len (val) for key, val, in test_dict.items()}
print ("The dictionary average is : " + str (res))
|
Output :
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
import statistics
test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = dict ()
for key in test_dict:
res[key] = float (statistics.mean(test_dict[key]))
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}
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))
|
Output
{'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)
|
Output
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
import numpy as np
test_dict = { 'Gfg' : [ 6 , 7 , 5 , 4 ], 'is' : [ 10 , 11 , 2 , 1 ], 'best' : [ 12 , 1 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = {key: np.mean(val) for key, val in test_dict.items()}
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.
Similar Reads
Mean of Tuple List - Python
We are give list of tuples we need to find the mean of each tuples. For example, a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] we need to find mean of each tuples so that output should be (4.0, 5.0, 6.0). Using sum() and len()We can find the mean of a tuple list by summing corresponding elements using sum()
2 min read
Python - Dictionary Values Mean
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
4 min read
Python | Variance of List
While working with Python, we can have a problem in which we need to find variance of a list cumulative. This problem is common in Data Science domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loop + formula The simpler manner to approach this problem is to e
4 min read
Python | Column Mean in tuple list
Sometimes, while working with records, we can have a problem in which we need to average all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using sum() + l
4 min read
Python - Kth Index Tuple List Mean
Sometimes, while working with Python tuple, we can have a problem in which we need to compute average of any particular index of tuples in a list. This kind of problem can have application in data domain such as web development. Let's discuss certain ways in which this task can be performed. Input :
6 min read
Python | Min and Max value in list of tuples
The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python | Harmonic Mean of List
While working with Python, we can have a problem in which we need to find harmonic mean of a list cumulative. This problem is common in Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using loop + formula The simpler manner to approach this problem is
5 min read
Find Median of List in Python
Sometimes, while working with Python list we can have a problem in which we need to find Median of list. This problem is quite common in the mathematical domains and generic calculations. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "~" operator This task
4 min read
Make a Set of Lists in Python
Creating a set of lists typically involves defining multiple lists and organizing them into a structure, such as a dictionary or another list. In this article, we will see how we can make a set of lists in Python. Table of Content Creating a Set of List Using list() FunctionMake A Set Of Lists Using
3 min read
Python - Values frequencies of key
Sometimes, while working with Python dictionary lists, one can have a problem in which we require to find the frequency of all the values occurring in a specific key in dictionary list. This can have application across many domains including web development. Let's discuss certain ways in which this
7 min read