Python – Maximum record value key in dictionary
Last Updated :
14 May, 2023
Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task can be performed.
Method #1: Using loop
This is a brute-force way in which this task can be performed. In this, we iterate through each key for keys and assign to max, the maximum of a required key in the nested record.
Python3
test_dict = { 'gfg' : { 'Manjeet' : 5 , 'Himani' : 10 },
'is' : { 'Manjeet' : 8 , 'Himani' : 9 },
'best' : { 'Manjeet' : 10 , 'Himani' : 15 }}
print ( "The original dictionary is : " + str (test_dict))
key = 'Himani'
res = None
res_max = 0
for sub in test_dict:
if test_dict[sub][key] > res_max:
res_max = test_dict[sub][key]
res = sub
print ( "The required key is : " + str (res))
|
Output
The original dictionary is : {'gfg': {'Manjeet': 5, 'Himani': 10}, 'is': {'Manjeet': 8, 'Himani': 9}, 'best': {'Manjeet': 10, 'Himani': 15}}
The required key is : best
Time complexity: O(n), where n is the number of sub-dictionaries in the main dictionary.
Auxiliary space: O(1), as we are using constant space variables to keep track of the maximum value and corresponding key.
Method #2: Using max() + lambda function
This is a one-liner approach to solving this problem. In this, we perform the task of iteration using max key argument, passing a lambda function that binds the required logic.
Python3
test_dict = { 'gfg' : { 'Manjeet' : 5 , 'Himani' : 10 },
'is' : { 'Manjeet' : 8 , 'Himani' : 9 },
'best' : { 'Manjeet' : 10 , 'Himani' : 15 }}
print ( "The original dictionary is : " + str (test_dict))
key = 'Himani'
res = max (test_dict, key = lambda sub: test_dict[sub][key])
print ( "The required key is : " + str (res))
|
Output
The original dictionary is : {'gfg': {'Manjeet': 5, 'Himani': 10}, 'is': {'Manjeet': 8, 'Himani': 9}, 'best': {'Manjeet': 10, 'Himani': 15}}
The required key is : best
Time Complexity: O(n), where n is the number of sub-dictionaries in the main dictionary.
Auxiliary Space: O(1)
Method 3: Use the built-in function reduce from the functools module
Step-by-step approach:
- Import the reduce function from the functools module.
- Define a function find_max_key that takes two arguments x and y, representing two key-value pairs from the dictionary.
- Within the function, compare the values of x and y for the given key and return the key-value pair with the maximum value.
- Call the reduce function on the dictionary with the find_max_key function as the first argument and the test_dict.items() as the second argument.
- Extract the key from the resulting maximum value key-value pair.
- Print the result.
Below is the implementation of the above approach:
Python3
from functools import reduce
test_dict = { 'gfg' : { 'Manjeet' : 5 , 'Himani' : 10 },
'is' : { 'Manjeet' : 8 , 'Himani' : 9 },
'best' : { 'Manjeet' : 10 , 'Himani' : 15 }}
print ( "The original dictionary is : " + str (test_dict))
key = 'Himani'
def find_max_key(x, y):
return x if x[ 1 ][key] > y[ 1 ][key] else y
max_key = reduce (find_max_key, test_dict.items())[ 0 ]
print ( "The required key is : " + str (max_key))
|
Output
The original dictionary is : {'gfg': {'Manjeet': 5, 'Himani': 10}, 'is': {'Manjeet': 8, 'Himani': 9}, 'best': {'Manjeet': 10, 'Himani': 15}}
The required key is : best
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1), as we are not using any additional data structures to store intermediate results.
Method #4: Using list comprehension and max() function
This approach uses list comprehension to extract the values of the specific key (‘Himani’ in this case) from each sub-dictionary of the main dictionary. The max() function is then used to find the maximum value among those extracted values, and the index() function is used to find the index of the sub-dictionary containing that maximum value. Finally, the keys() function is used to extract the keys of the main dictionary, and the key at the same index as the maximum value is returned.
Python3
test_dict = { 'gfg' : { 'Manjeet' : 5 , 'Himani' : 10 },
'is' : { 'Manjeet' : 8 , 'Himani' : 9 },
'best' : { 'Manjeet' : 10 , 'Himani' : 15 }}
print ( "The original dictionary is : " + str (test_dict))
key = 'Himani'
vals = [sub[key] for sub in test_dict.values()]
max_val = max (vals)
idx = vals.index(max_val)
res = list (test_dict.keys())[idx]
print ( "The required key is : " + str (res))
|
Output
The original dictionary is : {'gfg': {'Manjeet': 5, 'Himani': 10}, 'is': {'Manjeet': 8, 'Himani': 9}, 'best': {'Manjeet': 10, 'Himani': 15}}
The required key is : best
Time complexity: O(n), where n is the number of sub-dictionaries in the main dictionary.
Auxiliary space: O(n), where n is the number of sub-dictionaries in the main dictionary, as a list of all values of the key ‘Himani’ is created.
Method #5: Using heapq:
Algorithm:
- Import the required modules.
- Initialize the dictionary.
- Initialize the search key variable.
- Use the max() function with the lambda function to find the key with the maximum value for the given search key.
- Print the result.
Python3
import heapq
test_dict = { 'gfg' : { 'Manjeet' : 5 , 'Himani' : 10 },
'is' : { 'Manjeet' : 8 , 'Himani' : 9 },
'best' : { 'Manjeet' : 10 , 'Himani' : 15 }}
print ( "The original dictionary is : " + str (test_dict))
search_key = 'Himani'
max_key = max (test_dict, key = lambda k: test_dict[k][search_key])
print ( "The required key is : " + str (max_key))
|
Output
The original dictionary is : {'gfg': {'Manjeet': 5, 'Himani': 10}, 'is': {'Manjeet': 8, 'Himani': 9}, 'best': {'Manjeet': 10, 'Himani': 15}}
The required key is : best
Time Complexity: O(n log n) where n is the number of items in the dictionary. The max() function is used to find the maximum value key, which has a time complexity of O(n log n) due to the use of a heap data structure.
Auxiliary Space: O(n) as the dictionary and the variables used have a space complexity of O(n).
Please Login to comment...