Python - Cumulative Mean of Dictionary keys
Last Updated :
02 May, 2023
Given the dictionary list, our task is to write a Python Program to extract the mean of all keys.
Input : test_list = [{'gfg' : 34, 'is' : 8, 'best' : 10},
{'gfg' : 1, 'for' : 10, 'geeks' : 9, 'and' : 5, 'best' : 12},
{'geeks' : 8, 'find' : 3, 'gfg' : 3, 'best' : 8}]
Output : {'gfg': 12.666666666666666, 'is': 8, 'best': 10, 'for': 10, 'geeks': 8.5, 'and': 5, 'find': 3}
Explanation : best has 3 values, 10, 8 and 12, their mean computed to 10, hence in result.
Input : test_list = [{'gfg' : 34, 'is' : 8, 'best' : 10},
{'gfg' : 1, 'for' : 10, 'and' : 5, 'best' : 12},
{ 'find' : 3, 'gfg' : 3, 'best' : 8}]
Output : {'gfg': 12.666666666666666, 'is': 8, 'best': 10, 'for': 10, 'and': 5, 'find': 3}
Explanation : best has 3 values, 10, 8 and 12, their mean computed to 10, hence in result.
Method #1 : Using mean() + loop
In this, for extracting each list loop is used and all the values are summed and memorized using a dictionary. Mean is extracted later by dividing by the occurrence of each key.
Python3
# Python3 code to demonstrate working of
# Cumulative Keys Mean in Dictionary List
# Using loop + mean()
from statistics import mean
# initializing list
test_list = [{'gfg' : 34, 'is' : 8, 'best' : 10},
{'gfg' : 1, 'for' : 10, 'geeks' : 9, 'and' : 5, 'best' : 12},
{'geeks' : 8, 'find' : 3, 'gfg' : 3, 'best' : 8}]
# printing original list
print("The original list is : " + str(test_list))
res = dict()
for sub in test_list:
for key, val in sub.items():
if key in res:
# combining each key to all values in
# all dictionaries
res[key].append(val)
else:
res[key] = [val]
for key, num_l in res.items():
res[key] = mean(num_l)
# printing result
print("The Extracted average : " + str(res))
Output:
The original list is : [{'gfg': 34, 'is': 8, 'best': 10}, {'gfg': 1, 'for': 10, 'geeks': 9, 'and': 5, 'best': 12}, {'geeks': 8, 'find': 3, 'gfg': 3, 'best': 8}]
The Extracted average : {'gfg': 12.666666666666666, 'is': 8, 'best': 10, 'for': 10, 'geeks': 8.5, 'and': 5, 'find': 3}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using defaultdict() + mean()
In this, the task of memorizing is done using defaultdict(). This reduces one conditional check and makes the code more concise.
Python3
# Python3 code to demonstrate working of
# Cumulative Keys Mean in Dictionary List
# Using defaultdict() + mean()
from statistics import mean
from collections import defaultdict
# initializing list
test_list = [{'gfg' : 34, 'is' : 8, 'best' : 10},
{'gfg' : 1, 'for' : 10, 'geeks' : 9, 'and' : 5, 'best' : 12},
{'geeks' : 8, 'find' : 3, 'gfg' : 3, 'best' : 8}]
# printing original list
print("The original list is : " + str(test_list))
# defaultdict reduces step to memorize.
res = defaultdict(list)
for sub in test_list:
for key, val in sub.items():
res[key].append(val)
res = dict(res)
for key, num_l in res.items():
# computing mean
res[key] = mean(num_l)
# printing result
print("The Extracted average : " + str(res))
Output:
The original list is : [{'gfg': 34, 'is': 8, 'best': 10}, {'gfg': 1, 'for': 10, 'geeks': 9, 'and': 5, 'best': 12}, {'geeks': 8, 'find': 3, 'gfg': 3, 'best': 8}]
The Extracted average : {'gfg': 12.666666666666666, 'is': 8, 'best': 10, 'for': 10, 'geeks': 8.5, 'and': 5, 'find': 3}
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #3: Using pandas library
- Import the pandas library.
- Create a pandas DataFrame from the test_list.
- Use the melt function to transform the DataFrame from wide to long format, with one row for each key-value pair.
- Use the groupby function to group the DataFrame by the keys and calculate the mean of the values for each key.Convert the resulting pandas Series to a dictionary.
-
Python3
import pandas as pd
# initializing list
test_list = [{'gfg' : 34, 'is' : 8, 'best' : 10},
{'gfg' : 1, 'for' : 10, 'geeks' : 9, 'and' : 5, 'best' : 12},
{'geeks' : 8, 'find' : 3, 'gfg' : 3, 'best' : 8}]
# create pandas DataFrame from test_list
df = pd.DataFrame(test_list)
# transform DataFrame from wide to long format
df = df.melt(var_name='key', value_name='value')
# group DataFrame by keys and calculate mean of values for each key
res = df.groupby('key').mean()['value'].to_dict()
# print result
print("The Extracted average : " + str(res))
Output:
The Extracted average : {'and': 5.0, 'best': 10.0, 'find': 3.0, 'for': 10.0, 'geeks': 8.5, 'gfg': 12.666666666666666, 'is': 8.0}
Time complexity: O(n*logn), where n is the total number of key-value pairs in the test_list.
Auxiliary space: O(n), where n is the total number of key-value pairs in the test_list.
Method #4: using a list comprehension and the setdefault() method
- Create a list of dictionaries test_list.
- Create an empty dictionary res.
- Loop over each dictionary d in test_list.
- Loop over each key-value pair (key, val) in d.
- If the key key is not in res, set its value to an empty list. Append the value val to the list associated with the key key in the res dictionary.
- Create a new dictionary res_mean.
- Loop over each key-value pair (key, val) in the res dictionary.
- Compute the mean of the values val associated with the key key using the mean function from the statistics module.
- Add a new key-value pair to the res_mean dictionary with the key key and the value equal to the mean value computed in step 8.
- Print the res_mean dictionary as a string, with a message indicating that it contains the extracted average values.
Python3
from statistics import mean
test_list = [{'gfg': 34, 'is': 8, 'best': 10},
{'gfg': 1, 'for': 10, 'geeks': 9,
'and': 5, 'best': 12},
{'geeks': 8, 'find': 3, 'gfg': 3, 'best': 8}]
res = {}
for d in test_list:
for key, val in d.items():
res.setdefault(key, []).append(val)
res_mean = {key: mean(val) for key, val in res.items()}
print("The Extracted average : " + str(res_mean))
OutputThe Extracted average : {'gfg': 12.666666666666666, 'is': 8, 'best': 10, 'for': 10, 'geeks': 8.5, 'and': 5, 'find': 3}
Time complexity: O(nk), where n is the number of dictionaries in test_list and k is the average number of keys in each dictionary.
Auxiliary space: O(mk), where m is the number of unique keys in all the dictionaries in test_list and k is the average number of values associated with each key.
Similar Reads
Python - Cumulative product of dictionary value lists 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 product 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 cer
4 min read
Python - Count Dictionary Items In Python, dictionaries are one of the most widely used data structures. They allow us to store data in key-value pairs where each key maps to a value. In this article, weâll explore how to count dictionary items in Python using different methods including built-in functions and loops.Counting Total
3 min read
Python - Summation of Custom nested keys in Dictionary Given a dictionary with keys as nested dictionaries, find the sum of values of certain custom keys inside the nested dictionary. Input : test_dict = {'Gfg' : {1 : 6, 5: 9, 9: 12}, 'is' : {1 : 9, 5: 7, 9: 2}, 'best' : {1 : 3, 5: 4, 9: 14}}, sum_key = [1] Output : 18 Explanation : 6 + 9 + 3 = 18, only
10 min read
Python Program to calculate Dictionaries frequencies Given a list of dictionaries, the task here is to write a python program to extract dictionary frequencies of each dictionary. Input : test_list = [{'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 6, 'is' : 3, 'best' : 8}, {'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 1, 'is' : 1, 'best' : 9}, {'gfg' : 6,
6 min read
Get Maximum of Each Key Dictionary List - Python We are given a list of dictionaries and our task is to find the maximum value for each key across all dictionaries. If a key appears in multiple dictionaries we take the highest value among them. For example: a = [{'a': 3, 'b': 8}, {'a': 10, 'b': 2, 'c': 5}, {'c': 12, 'a': 7}] then the output will b
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