Python – Extract ith Key’s Value of K’s Maximum value dictionary
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)) |
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), where n is the number of dictionaries in the list. This is because the max function is used to iterate over the entire list to find the maximum value based on the key “K”. The get function has an average time complexity of O(1) since it uses a hash table for lookup. Therefore, the overall time complexity of this code is O(n).
Auxiliary space: O(1), as it only uses a constant amount of extra space to store the result.
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)) |
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), where n is the length of the list of dictionaries.
Auxiliary space: O(1), since only a constant amount of extra space is used for the variables K, i, and res.
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)) |
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)
Method 4: Using list comprehension:
- Define the test_list containing multiple dictionaries.
- Initialize the K and i variables with the keys that we want to use for comparison and extraction, respectively.
- Use list comprehension to extract the values of K’s key from the dictionaries in the test_list, and store them in a list called k_values.
- Find the maximum value in the k_values list using the built-in max() function and store it in max_k_value.
- Use another list comprehension to extract the dictionary that contains K’s maximum value, and store it in max_k_dict. We use [0] at the end of this comprehension because it returns a list containing only one dictionary, and we want to extract the dictionary itself (not the list) from it.
- Extract the value of the ith key from the max_k_dict using indexing, and store it in the res variable.
- Print the result.
Python3
# 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" # extracting values of K's key from the test_list using list comprehension k_values = [d[K] for d in test_list] # finding the maximum value of K's key max_k_value = max (k_values) # extracting the dictionary that contains K's maximum value using list comprehension max_k_dict = [d for d in test_list if d[K] = = max_k_value][ 0 ] # extracting the ith key's value from the dictionary res = max_k_dict[i] # printing result print ( "The required value : " + str (res)) |
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), where n is the number of dictionaries in the test_list.
Auxiliary Space: O(N)
Method #5: Using a loop and if statements
- Initialize a variable max_val to -infinity and a variable res to None.
- Iterate through each dictionary in the test_list using a for loop.
- Inside the loop, check if the value of the key K in the current dictionary is greater than max_val. If it is, assign the value of the key K to max_val, and assign the value of the key i to res.
- Return res.
Python3
test_list = [{ "Gfg" : 3 , "is" : 9 , "best" : 10 }, { "Gfg" : 8 , "is" : 11 , "best" : 19 }, { "Gfg" : 9 , "is" : 16 , "best" : 1 }] # initializing K K = "best" # initializing i i = "Gfg" max_val = float ( '-inf' ) res = None for d in test_list: if K in d and d[K] > max_val: max_val = d[K] res = d[i] print ( "The required value : " + str (res)) |
The required value : 8
Time complexity: O(n), where n is the length of the test_list. We need to iterate through all the dictionaries in the list to find the maximum value of K.
Auxiliary space: O(1). We are only using a few variables to store the maximum value and the corresponding key’s value, so the space used is constant.
Please Login to comment...