Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Maximum value assignment in Nested Dictionary

Python – Maximum value assignment in Nested Dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign to the outer key, the item with maximum value in inner keys. This kind of problem can occur in day-day programming and web development domains. Let’s discuss a way in which this task can be performed.

Input : test_dict = {‘Manjeet’: {‘English’: 19, ‘Maths’: 1}, ‘Himani’: {‘English’: 18, ‘Maths’: 17}} 
Output : [{‘Manjeet’: (‘English’, 19)}, {‘Himani’: (‘English’, 18)}] 

Input : test_dict = {‘Manjeet’ : {‘Maths’:10}} 
Output : [{‘Manjeet’: (‘Maths’, 10)}]

Method : Using Counter().most_common() + items() + loop The combination of above functions constitute the brute way to solve this problem. In this, we extract the maximum element using most_common() and items() is used to extract key-value pair. 

Python3




# Python3 code to demonstrate working of
# Maximum value assignment in Nested Dictionary
# Using Counter().most_common() + items() + loop
from collections import Counter
 
# initializing dictionary
test_dict = {'Manjeet' : {'Maths':1, 'English':0, 'Physics':2, 'Chemistry':3},
            'Akash' : {'Maths':0, 'English':0, 'Physics':3, 'Chemistry':2},
            'Nikhil': {'Maths':4, 'English':2, 'Physics':2, 'Chemistry':3},
            'Akshat': {'Maths':1, 'English':0, 'Physics':2, 'Chemistry':0}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Maximum value assignment in Nested Dictionary
# Using Counter().most_common() + items() + loop
res = []
for key, val in test_dict.items():
    cnt = Counter(val)
    res.append({key : cnt.most_common(1)[0]})
     
# printing result
print("Maximum element key : " + str(res))


Output

The original dictionary : {'Manjeet': {'Maths': 1, 'English': 0, 'Physics': 2, 'Chemistry': 3}, 'Akash': {'Maths': 0, 'English': 0, 'Physics': 3, 'Chemistry': 2}, 'Nikhil': {'Maths': 4, 'English': 2, 'Physics': 2, 'Chemistry': 3}, 'Akshat': {'Maths': 1, 'English': 0, 'Physics': 2, 'Chemistry': 0}}
Maximum element key : [{'Manjeet': ('Chemistry', 3)}, {'Akash': ('Physics', 3)}, {'Nikhil': ('Maths', 4)}, {'Akshat': ('Physics', 2)}]

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

Using the built-in max() function to find the maximum value in a nested dictionary:

Approach:

  • Initialize an empty list to store the output.
  • Iterate through the nested dictionary using loops.
  • For each key in the outer dictionary, use the max() function to find the maximum value in the inner dictionary.
  • Append a new dictionary to the output list with the key and value pair of the maximum value.
  • Return the output list.

Python3




def max_value(test_dict):
    output = []
    for key in test_dict:
        max_key, max_value = max(test_dict[key].items(), key=lambda x: x[1])
        output.append({key: (max_key, max_value)})
    return output 
test_dict = {'Manjeet': {'English': 19, 'Maths': 1}, 'Himani': {'English': 18, 'Maths': 17}}
print(max_value(test_dict)) # Output: [{'Manjeet': ('English', 19)}, {'Himani': ('English', 18)}]
 
test_dict = {'Manjeet': {'Maths': 10}}
print(max_value(test_dict)) # Output: [{'Manjeet': ('Maths', 10)}]


Output

[{'Manjeet': ('English', 19)}, {'Himani': ('English', 18)}]
[{'Manjeet': ('Maths', 10)}]

Time Complexity: O(n^2) where n is the number of key-value pairs in the nested dictionary. This is because we are iterating through the nested dictionary using loops and using the max() function for each inner dictionary.

Space Complexity: O(n) where n is the number of key-value pairs in the nested dictionary. This is because we are storing the output in a list.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments