Python – Maximum available value Dictionaries
Last Updated :
23 Apr, 2023
Given list of dictionaries and a list, extract all the dictionaries which contain maximum available value of key from list.
Input : test_list [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 19}, {“Gfg” : 2, “is” : 16, “best” : 10}], K = “best”, arg_list = [10, 7, 6, 12] Output : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 2, ‘is’: 16, ‘best’: 10}] Explanation : Maximum available value of “best” is 19, but not present in list, hence next max. is 10, all dictionaries corresponding are returned. Input : test_list [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 19}], K = “Gfg”, arg_list = [10, 7, 6, 12] Output : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}] Explanation : Maximum value present in this case is 6, hence returned.
Method #1 : Using loop
This is brute way in which this problem can be solved. In this, first maximum value is obtained from dictionary values, which is also present in provided list. Post that all the dictionaries, which have that value are extracted.
Python3
test_list = [{ "Gfg" : 6 , "is" : 9 , "best" : 10 },
{ "Gfg" : 8 , "is" : 11 , "best" : 19 },
{ "Gfg" : 2 , "is" : 16 , "best" : 10 },
{ "Gfg" : 12 , "is" : 1 , "best" : 8 },
{ "Gfg" : 22 , "is" : 6 , "best" : 8 }]
print ( "The original list : " + str (test_list))
K = "best"
arg_list = [ 10 , 7 , 6 , 12 ]
max_ele = 0
for sub in test_list:
if sub[K] in arg_list:
max_ele = max (sub[K], max_ele)
res = [sub for sub in test_list if sub[K] = = max_ele]
print ( "The extracted dictionaries : " + str (res))
|
Output
The original list : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 8, ‘is’: 11, ‘best’: 19}, {‘Gfg’: 2, ‘is’: 16, ‘best’: 10}, {‘Gfg’: 12, ‘is’: 1, ‘best’: 8}, {‘Gfg’: 22, ‘is’: 6, ‘best’: 8}] The extracted dictionaries : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 2, ‘is’: 16, ‘best’: 10}]
Time Complexity: O(n), where n is the values in dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method #2: Using list comprehension and built-in functions
Python3
test_list = [{ "Gfg" : 6 , "is" : 9 , "best" : 10 },
{ "Gfg" : 8 , "is" : 11 , "best" : 19 },
{ "Gfg" : 2 , "is" : 16 , "best" : 10 },
{ "Gfg" : 12 , "is" : 1 , "best" : 8 },
{ "Gfg" : 22 , "is" : 6 , "best" : 8 }]
K = "best"
arg_list = [ 10 , 7 , 6 , 12 ]
values_to_find = set (arg_list)
filtered_list = [d for d in test_list if d[K] in values_to_find]
max_value = max (d[K] for d in filtered_list)
result_list = [d for d in filtered_list if d[K] = = max_value]
print ( "The original list : " + str (test_list))
print ( "The extracted dictionaries : " + str (result_list))
print ( "The maximum value of key '" + K + "' is: " + str (max_value))
|
Output
The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The extracted dictionaries : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 2, 'is': 16, 'best': 10}]
The maximum value of key 'best' is: 10
Time complexity: O(n), where n is the length of the list of dictionaries.
Auxiliary space: O(n), where n is the length of the list of dictionaries.
Share your thoughts in the comments
Please Login to comment...