![]() |
VOOZH | about |
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.
The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8Time 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.
The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8Time 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
The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8Time Complexity : O(N*N)
Auxiliary Space : O(N)
Method 4: Using list comprehension:
The original list : [{'Gfg': 3, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 9, 'is': 16, 'best': 1}]
The required value : 8Time 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
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.
Method#6: Using Recursive method.
The algorithm for the recursive method to extract the value of key i from the dictionary with the maximum value of key K in a list of dictionaries is as follows:
The required value : 8
The time complexity of this algorithm is O(n), where n is the number of dictionaries in test_list. This is because we need to iterate over all dictionaries in test_list to find the maximum value of key K.
The auxiliary space of this algorithm is O(n), where n is the number of dictionaries in test_list. This is because we need to store n recursive calls on the call stack.
Method #7: Using the built-in sorted() function
The required value : 8
Time complexity: O(n log n), where n is the length of test_list due to the use of the sorted() function.
Auxiliary space: O(n), due to the creation of a sorted list with the same length as test_list.