Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Extract ith Key’s Value of K’s Maximum value dictionary

Improve Article
Save Article
  • Last Updated : 21 Jan, 2023
Improve Article
Save Article

Given Dictionary List, extract i’th keys value depending upon Kth key’s maximum value.

Input : test_list = [{“Gfg” : 3, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 19}, {“Gfg” : 9, “is” : 16, “best” : 1}], K = “best”, i = “is” Output : 11 Explanation : best is max at 19, its corresponding “is” value is 11. Input : test_list = [{“Gfg” : 3, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 19}, {“Gfg” : 9, “is” : 16, “best” : 1}], K = “Gfg”, i = “is” Output : 16 Explanation : Gfg is max at 9, its corresponding “is” value is 16.

Method #1 : Using max() + lambda

The combination of above functions can be used to solve this problem. In this, we extract max of kth key using max() and lambda. Then ith key is extracted from extracted dictionary.

Python3




# Python3 code to demonstrate working of
# Extract ith Key's Value of K's Maximum value dictionary
# Using max() + lambda
 
# initializing lists
test_list = [{"Gfg" : 3, "is" : 9, "best" : 10},
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 9, "is" : 16, "best" : 1}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "best"
 
# initializing i
i = "Gfg"
 
# using get() to handle missing key, assigning lowest value
res = max(test_list, key = lambda ele : ele.get(K, 0))[i]
     
# printing result
print("The required value : " + str(res))

Output

The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8

Method #2 : Using max() + external function

This is yet another way to solve this problem. This computes in similar way as above method, just the difference being of usage of custom comparator rather than lambda function.

Python3




# Python3 code to demonstrate working of
# Extract ith Key's Value of K's Maximum value dictionary
# Using max() + external function
 
# custom function as comparator
def cust_fnc(ele):
    return ele.get(K, 0)
 
# initializing lists
test_list = [{"Gfg" : 3, "is" : 9, "best" : 10},
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 9, "is" : 16, "best" : 1}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "best"
 
# initializing i
i = "Gfg"
 
# using get() to handle missing key, assigning lowest value
res = max(test_list, key = cust_fnc)[i]
     
# printing result
print("The required value : " + str(res))

Output

The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8

Method #3 : Using max() and for loops

Python3




# Python3 code to demonstrate working of
# Extract ith Key's Value of K's Maximum value dictionary
 
 
# initializing lists
test_list = [{"Gfg" : 3, "is" : 9, "best" : 10},
            {"Gfg" : 8, "is" : 11, "best" : 19},
            {"Gfg" : 9, "is" : 16, "best" : 1}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "best"
 
# initializing i
i = "Gfg"
x=[]
for j in test_list:
    x.append(j[K])
a=max(x)
for j in test_list:
    if(j[K]==a):
        res=j[i]   
# printing result
print("The required value : " + str(res))

Output

The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8

Time Complexity : O(N*N)

Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!