Python Program to get value of a dictionary given by index of maximum value of given key
Given a dictionary with value as a list, the task is to write a Python program that can find the maximum of any one key and output a similar index column of other key’s values.
Approach :
- Get the maximum of the given key.
- Get the index of the maximum element found.
- Check if the index of max is present in the search key, if not, return Result not Possible.
- Get the element on the search key ( opt_key), which is at the index found in Step 2.
Input : test_dict = {“gfg” : [4, 1, 6], “is” : [1, 4, 8], “best” : [9, 10, 1]}, max_search = “best”, opt_key = “gfg”
Output : 1
Explanation : 10 is maximum element in best key, which corresponds to 1st index. In gfg, 1st index is 1, hence 1.
Input : test_dict = {“gfg” : [4, 10, 6], “is” : [1, 4, 8], “best” : [9, 10, 1]}, max_search = “best”, opt_key = “gfg”
Output : 10
Explanation : 10 is maximum element in best key, which corresponds to 1st index. In gfg, 1st index is 10, hence 10.
Method 1 : Using index(), max() and lambda
In this, we find the maximum of max search key using max() and get its index using index(), and then get the value of index in output key.
Example:
Python3
# initializing dictionarytest_dict = {"gfg": [4], "is": [1, 4, 8], "best": [9, 10]} # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # initializing max_search keymax_search = "best" # initializing output keyopt_key = "gfg" # handling case in which maximum index is not # present in output keyif len(test_dict[opt_key]) <= test_dict[max_search].index( max(test_dict[max_search])): res = "Result not possible" # using max() to get of search keyelse: res = max(test_dict[opt_key], key=lambda sub: test_dict[max_search] [test_dict[opt_key].index(sub)]) # printing resultprint("The required output : " + str(res)) |
Output:
The original dictionary is : {‘gfg’: [4], ‘is’: [1, 4, 8], ‘best’: [9, 10]}
The required output : Result not possible
Method 2 : Using zip() and max()
In this, the lists of both the search and output keys are combined using zip(), post that a maximum of one list is found and the corresponding index of output key is returned.
Example:
Python3
# initializing dictionarytest_dict = {"gfg": [4, 1, 6], "is": [1, 4, 8], "best": [9, 10, 1]} # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # initializing max_search keymax_search = "best" # initializing output keyopt_key = "gfg" # handling case in which maximum index is not present # in output keyif len(test_dict[opt_key]) <= test_dict[max_search].index( max(test_dict[max_search])): res = "Result not possible" # using max() to get of search key# zip() used for combining listselse: res = max(zip(test_dict[opt_key], test_dict[max_search]), key=lambda sub: sub[1])[0] # printing resultprint("The required output : " + str(res)) |
Output:
The original dictionary is : {‘gfg’: [4, 1, 6], ‘is’: [1, 4, 8], ‘best’: [9, 10, 1]}
The required output : 1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


