Open In App

Python – Inner Nested Value List Mean in Dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

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}}

Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

METHOD 4:Using the map() function with a lambda function

APPROACH:

The function inner_nested_mean_5 calculates the mean of each inner nested value list in a given dictionary of dictionaries and returns a new dictionary with the same keys and mean values. It achieves this using a combination of lambda functions, map, and dictionary comprehension.

ALGORITHM:

1. Create a lambda function that computes the mean of a list using the sum() and len() functions
2. Iterate over the keys of the dictionary and the nested dictionary using map() and the lambda function
3. Convert the result to a dictionary using dict()
4. Return the modified dictionary

Python3




def inner_nested_mean_5(d):
    mean_fn = lambda l: sum(l)/len(l)
    return dict(map(lambda k1: (k1, dict(map(lambda k2: (k2, mean_fn(d[k1][k2])), d[k1].keys()))), d.keys()))
 
# Test the function with the given input
d = {'Gfg': {'a': [1, 5, 6, 7], 'b': [6, 7, 8, 9]}, 'is': {'best': [2, 8, 9, 0]}}
result = inner_nested_mean_5(d)
print(result)  # Output: {'Gfg': {'a': 4.75, 'b': 7.5}, 'is': {'best': 4.75}}


Output

{'Gfg': {'a': 4.75, 'b': 7.5}, 'is': {'best': 4.75}}

Time complexity: O(n^2) where n is the number of nested lists in the dictionary
Space complexity: O(1) 



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads