Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Inner Nested Value List Mean in Dictionary

Improve Article
Save Article
  • Last Updated : 04 Feb, 2023
Improve Article
Save Article

Sometimes, while working with Python Dictionaries, we can have a problem in which we need to extract the mean of nested value lists in dictionary. This problem can have application in many domains including web development and competitive programming. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using mean() + loop The combination of above functions provide the brute way to solve this problem. In this, we perform the task of finding mean using inbuilt mean() library and iterate for nesting using loop. 

Python3




# Python3 code to demonstrate working of
# Inner Nested Value List Mean in Dictionary
# Using mean() + loop
from statistics import mean
 
# initializing dictionary
test_dict = {'Gfg' : {'a' : [1, 5, 6, 7], 'b' : [6, 7, 8, 9]}, 'is' : {'best' :[2, 8, 9, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Inner Nested Value List Mean in Dictionary
# Using mean() + loop
for sub in test_dict.values():
    for key in sub:
        sub[key] = mean(sub[key])
 
# printing result
print("The modified dictionary : " + str(test_dict))

Output : 

The original dictionary : {‘Gfg’: {‘a’: [1, 5, 6, 7], ‘b’: [6, 7, 8, 9]}, ‘is’: {‘best’: [2, 8, 9, 0]}} The modified dictionary : {‘Gfg’: {‘a’: 4.75, ‘b’: 7.5}, ‘is’: {‘best’: 4.75}}

Time Complexity: O(N), where N is the total number of elements in the dictionary, as we are iterating through the values of the dictionary and for each value, we are computing the mean of its corresponding list, which takes O(n) time, where n is the length of the list.
Auxiliary Space: O(1), as we are using the mean function from the statistics library, which uses a constant amount of space to store temporary variables.

Method #2 : Using dictionary comprehension + mean() This is yet another way to solve this problem. In this, we perform similar task as above method. But difference is that in compact way and as one-liner. 

Python3




# Python3 code to demonstrate working of
# Inner Nested Value List Mean in Dictionary
# Using dictionary comprehension + mean()
from statistics import mean
 
# initializing dictionary
test_dict = {'Gfg' : {'a' : [1, 5, 6, 7], 'b' : [6, 7, 8, 9]}, 'is' : {'best' :[2, 8, 9, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Inner Nested Value List Mean in Dictionary
# Using dictionary comprehension + mean()
res = {idx: {key: mean(idx) for key, idx in j.items()} for idx, j in test_dict.items()}
 
# printing result
print("The modified dictionary : " + str(res))

Output : 

The original dictionary : {‘Gfg’: {‘a’: [1, 5, 6, 7], ‘b’: [6, 7, 8, 9]}, ‘is’: {‘best’: [2, 8, 9, 0]}} The modified dictionary : {‘Gfg’: {‘a’: 4.75, ‘b’: 7.5}, ‘is’: {‘best’: 4.75}}

Time Complexity: O(n), where n is the total number of elements in the dictionary. The dictionary comprehension used here requires a single pass through the dictionary to calculate the mean of each nested list.
Auxiliary Space: O(m), where m is the number of elements in the result dictionary. The result dictionary takes O(m) space in the memory as it stores the mean of each nested list.

Method #3 : Using sum()+len()+for loop

Python3




# Python3 code to demonstrate working of
# Inner Nested Value List Mean in Dictionary
 
# initializing dictionary
test_dict = {'Gfg' : {'a' : [1, 5, 6, 7], 'b' : [6, 7, 8, 9]}, 'is' : {'best' :[2, 8, 9, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Inner Nested Value List Mean in Dictionary
 
for sub in test_dict.values():
    for key in sub:
        sub[key] = sum(sub[key])/len(sub[key])
 
# printing result
print("The modified dictionary : " + str(test_dict))

Output

The original dictionary : {'Gfg': {'a': [1, 5, 6, 7], 'b': [6, 7, 8, 9]}, 'is': {'best': [2, 8, 9, 0]}}
The modified dictionary : {'Gfg': {'a': 4.75, 'b': 7.5}, 'is': {'best': 4.75}}

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!