Skip to content
Related Articles

Related Articles

Improve Article

Python – Key with Maximum element at Kth index in Dictionary Value List

  • Last Updated : 29 Jun, 2021

Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list.

Input : test_dict = {‘Gfg’ : [4, 6, 8, 2], 
‘is’ : [1, 4, 5, 9], 
‘best’ :[2, 3, 4, 10], 
‘for’ :[4, 5, 2, 1], 
‘geeks’ :[2, 10, 1, 8]}, K = 3 
Output : best 
Explanation : 2, 9, 10, 1, 8 are elements and 10 is maximum.
 

Input : test_dict = {‘Gfg’ : [4, 6, 8, 2], 
‘is’ : [1, 4, 5, 9], 
‘best’ :[2, 3, 4, 10], 
‘for’ :[4, 5, 2, 1], 
‘geeks’ :[2, 10, 1, 8]}, K = 2 
Output : Gfg 
Explanation : 8, 5, 4, 2, 1 are elements and 8 is maximum. 
 

Method 1 : Using sorted(), dictionary comprehension and lambda

In this, we perform reverse sort of all the list elements at the index K values along with its Key, and then output keys of maximum value extracted.



Example:

Python3




# Python3 code to demonstrate working of
# Key with Maximum element at Kth index 
# in Dictionary Value List Using sorted()
# + dictionary comprehension + lambda
  
# initializing dictionary
test_dict = {'Gfg': [4, 6],
             'is': [1, 4, 5, 9, 4, 5, 7],
             'best': [2, 3, 4, 10],
             'for': [4],
             'geeks': [2, 10, 1, 10]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K
K = 3
  
# sorted sorting all the values in reverse order
# Maximum element is found at 1st position
temp = sorted({key: val[K] if K <= len(val) else -1 for key,
               val in test_dict.items()}.items(),
              key=lambda sub: sub[1], reverse=True)
  
# getting all maximum keys in case of multiple
res = []
for idx, ele in enumerate(temp):
    res.append(temp[idx][0])
    if temp[idx][1] != temp[idx + 1][1]:
        break
  
# printing result
print("The extracted key : " + str(res))

Output:

The original dictionary is : {‘Gfg’: [4, 6, 8, 2, 5, 6], ‘is’: [1], ‘best’: [2, 3, 4, 10], ‘for’: [4, 5, 2, 1], ‘geeks’: [2, 10, 1, 10]}

The extracted key : [‘best’, ‘geeks’]

Method 2 : Using max() and generator expression

In this, we perform task of getting maximum using max(), and generator expression is used to iterate through all the keys in dictionary. At next step, we get all the keys matching the maximum element at K.

Example:

Python3




# Python3 code to demonstrate working of
# Key with Maximum element at Kth index 
# in Dictionary Value List Using max()
# + generator expression
  
# initializing dictionary
test_dict = {'Gfg': [4, 6, 8, 2, 5, 6],
             'is': [1],
             'best': [2, 3, 4, 10],
             'for': [4, 5, 2, 1],
             'geeks': [2, 10, 1, 10]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K
K = 3
  
# sorted sorting all the values in reverse order
# Maximum element is found at 1st position
# getting maximum
temp = max(test_dict[key][K] if K < len(
    test_dict[key]) else -1 for key in test_dict)
  
# getting all keys with maximum.
res = []
for key in test_dict:
    if K < len(test_dict[key]) and test_dict[key][K] == temp:
        res.append(key)
  
# printing result
print("The extracted key : " + str(res))

Output:

The original dictionary is : {‘Gfg’: [4, 6, 8, 2, 5, 6], ‘is’: [1], ‘best’: [2, 3, 4, 10], ‘for’: [4, 5, 2, 1], ‘geeks’: [2, 10, 1, 10]}

The extracted key : [‘best’, ‘geeks’]

 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 :