Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 18 Jul, 2021
Geek Week

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 dictionary
test_dict = {"gfg": [4], "is": [1, 4, 8], "best": [9, 10]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing max_search key
max_search = "best"
  
# initializing output key
opt_key = "gfg"
  
# handling case in which maximum index is not 
# present in output key
if 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
else:
    res = max(test_dict[opt_key], key=lambda sub: test_dict[max_search]
              [test_dict[opt_key].index(sub)])
  
# printing result
print("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 dictionary
test_dict = {"gfg": [4, 1, 6], "is": [1, 4, 8], "best": [9, 10, 1]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing max_search key
max_search = "best"
  
# initializing output key
opt_key = "gfg"
  
# handling case in which maximum index is not present 
# in output key
if 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 lists
else:
    res = max(zip(test_dict[opt_key], test_dict[max_search]),
              key=lambda sub: sub[1])[0]
  
# printing result
print("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




My Personal Notes arrow_drop_up
Recommended Articles
Page :