Python program to find Maximum value from dictionary whose key is present in the list
Last Updated :
27 Jul, 2023
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.
Similar Reads
Python program to find the key of maximum value tuples in a dictionary
Given a dictionary with values as tuples, the task is to write a python program to find the key of maximum value tuples. Examples: Input : test_dict = {'gfg' : ("a", 3), 'is' : ("c", 9), 'best' : ("k", 10), 'for' : ("p", 11), 'geeks' : ('m', 2)}Output : forExplanation : 11 is maximum value of tuple
6 min read
Python program to find second maximum value in Dictionary
Dictionaries in Python is same as Associative arrays or Maps in other languages. Unlike lists, dictionaries stores data in key-value pair.Let's see how to find the second maximum value in a Python dictionary.Method #1: Naive Approach: A general approach is to find the largest value of the dictionary
3 min read
Python program to sort a dictionary list based on the maximum value
Given list with dictionaries as elements, write a Python program to sort the dictionary on the basis maximum value in a key value pair of a dictionary. In simpler terms, first each element of a list (which is a dictionary) will be checked, meaning each key value pair will be compared to find the ele
5 min read
Python Program that displays the key of list value with maximum range
Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum. Range = Maximum number-Minimum number Input : test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]} Output : Best Explanation : 9 - 0 = 9, Maxim
5 min read
Remove Dictionary from List If Key is Equal to Value in Python
Removing dictionaries from a list based on a specific condition is a common task in Python, especially when working with data in list-of-dictionaries format. In this article, we will see various methods to Remove Dictionary from List If the Key is Equal to the Value in Python. Using filter()filter()
2 min read
Python - Key with Maximum element at Kth index in Dictionary Value List
Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10,
8 min read
Python - Extract Key's Value, if Key Present in List and Dictionary
Given a list, dictionary and a Key K, print the value of K from the dictionary if the key is present in both, the list and the dictionary. For example: a = ["Gfg", "is", "Good", "for", "Geeks"], d = {"Gfg": 5, "Best": 6} and K = "Gfg" then the output will be 5 as "Gfg" is present in both the list an
3 min read
Python program to find the sum of the value in the dictionary where the key represents the frequency
Given a dictionary with a values list, where the key represents frequency, compute the total occurrence of each value in values lists. Input : test_dict = {70 : [7, 4, 6], 50 : [6, 8, 5, 2]} Output : {7: 70, 4: 70, 6: 120, 8: 50, 5: 50, 2: 50} Explanation : 6 occurs in both keys, hence 70 + 50 = 120
3 min read
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 correspondin
9 min read
Python - Maximum record value key in dictionary
Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
6 min read