Python Program to get value of a dictionary given by index of maximum value of given key

18 Jul, 2021
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
Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Split string into list of characters