Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list.
Examples:
Input : test_dict = {“Gfg”: 4, “is” : 5, “best” : 10, “for” : 11, “geeks” : 3}, test_list = [“Gfg”, “best”, “geeks”]
Output : 10
Explanation : Max value is 11, but not present in list, 10 is of key best, which is also in list.
Input : test_dict = {“Gfg”: 4, “is” : 5, “best” : 10, “for” : 11, “geeks” : 3}, test_list = [“Gfg”, “best”, “geeks”, “for”]
Output : 11
Explanation : Max. value, 11, present in list as well.
Maximum value from dictionary Using loop
This is one of the ways in which this task can be performed. In this, we check for all the keys present in list and also maximum, then return the maximum available.
Python3
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
res = 0
for ele in test_list:
if ele in test_dict:
res = max (res, test_dict[ele])
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time complexity: O(n), where n is the number of elements in the test_list. The time complexity is O(n) because it loops through the test_list once and checks for each element if it exists in the test_dict using the in operator, which takes O(1) time on average.
Auxiliary Space: O(1), as it only uses a variable ‘res’ to store the maximum value and it does not increase with the size of the input.
Maximum value from dictionary Using max() + list comprehension
This is yet another way in which this task can be performed. In this, we extract maximum using max() and shorthand list comprehension is used to iterate through values.
Python3
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
res = max ([test_dict[ele] for ele in test_list
if ele in test_dict])
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time complexity: O(n), where n is the number of elements in the test_list. The time complexity is O(n) because the list comprehension will loop through the test_list once and check for each element if it exists in the test_dict using the in operator, which takes O(1) time on average. Then, it takes O(n) time to find the maximum value of the list comprehension.
Auxiliary Space: O(n), as the list comprehension will create a new list of the values of the test_dict keys that are present in test_list, which will take up O(n) space.
Maximum value from dictionary Using Counter() function
Python3
from collections import Counter
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
freq = Counter(test_list)
res = 0
for ele in test_dict:
if ele in freq.keys():
res = max (res, test_dict[ele])
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time Complexity: O(N)
Auxiliary Space: O(N)
Maximum value from dictionary Using reduce() method.
Python3
from functools import reduce
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
res = reduce ( lambda x, y: max (x, test_dict[y]), test_list, 0 )
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time Complexity: O(n)
Auxiliary Space: O(n)
Maximum value from dictionary Using map() and lambda function
Step-by-step approach:
- Initialize a dictionary test_dict.
- Initialize a list test_list.
- Use map() function to map the keys from the test_list to their corresponding values in the test_dict.
- Use lambda function to return the value of the key passed as an argument.
- Use max() function to get the maximum value from the list generated by map() function.
- Print the result.
Python3
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
res = max ( map ( lambda x: test_dict[x], test_list))
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time complexity: O(n), where n is the number of keys in the list test_list.
Auxiliary space: O(1), as only constant extra space is used.
Maximum value from dictionary Using the sorted() function
Step-by-step approach:
- Sort the list in descending order based on the values of the keys in the dictionary
- Get the first element of the sorted list, which will have the maximum value
- This method assumes that the keys in the list are present in the dictionary
Below is the implementation of the above approach:
Python3
test_dict = { "Gfg" : 4 , "is" : 5 , "best" : 9 ,
"for" : 11 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_list = [ "Gfg" , "best" , "geeks" ]
sorted_list = sorted (test_list, key = lambda x: test_dict[x], reverse = True )
res = test_dict[sorted_list[ 0 ]]
print ( "The required maximum : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 5, 'best': 9, 'for': 11, 'geeks': 3}
The required maximum : 9
Time complexity: O(n log n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jul, 2023
Like Article
Save Article