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
# Python3 code to demonstrate working of
# Maximum value from List keys
# Using loop
# initializing dictionary
test_dict = {"Gfg": 4, "is" : 5, "best" : 9,
"for" : 11, "geeks" : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing list
test_list = ["Gfg", "best", "geeks"]
res = 0
for ele in test_list:
# checking for key in dictionary
if ele in test_dict:
res = max(res, test_dict[ele])
# printing result
print("The required maximum : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum value from List keys
# Using max() + list comprehension
# initializing dictionary
test_dict = {"Gfg": 4, "is" : 5, "best" : 9,
"for" : 11, "geeks" : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing list
test_list = ["Gfg", "best", "geeks"]
# maximum is 11, but not present in list,
# hence 9 is output.
res = max([test_dict[ele] for ele in test_list
if ele in test_dict])
# printing result
print("The required maximum : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum value from List keys
from collections import Counter
# initializing dictionary
test_dict = {"Gfg": 4, "is": 5, "best": 9,
"for": 11, "geeks": 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing list
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])
# printing result
print("The required maximum : " + str(res))
OutputThe 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
#Python3 code to demonstrate working of
#Maximum value from List keys
#Using reduce
#importing reduce
from functools import reduce
#initializing dictionary
test_dict = {"Gfg": 4, "is" : 5, "best" : 9,
"for" : 11, "geeks" : 3}
#printing original dictionary
print("The original dictionary is : " + str(test_dict))
#initializing list
test_list = ["Gfg", "best", "geeks"]
#Using reduce() + lambda
res = reduce(lambda x, y: max(x, test_dict[y]), test_list, 0)
#printing result
print("The required maximum : " + str(res))
#this code contributed by tvsk
OutputThe 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
# Python3 code to demonstrate working of
# Maximum value from List keys
# Using map() and lambda function
# initializing dictionary
test_dict = {"Gfg": 4, "is" : 5, "best" : 9,
"for" : 11, "geeks" : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing list
test_list = ["Gfg", "best", "geeks"]
# using map() and lambda function to get the maximum value
res = max(map(lambda x: test_dict[x], test_list))
# printing result
print("The required maximum : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum value from List keys
# Using sorted() function
# initializing dictionary
test_dict = {"Gfg": 4, "is" : 5, "best" : 9,
"for" : 11, "geeks" : 3}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing list
test_list = ["Gfg", "best", "geeks"]
# using sorted() function to get the maximum value
sorted_list = sorted(test_list, key=lambda x: test_dict[x], reverse=True)
res = test_dict[sorted_list[0]]
# printing result
print("The required maximum : " + str(res))
OutputThe 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 find the highest 3 values in a dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Examples: Input : my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69} Output :
5 min read
Python Program to display keys with same values in a dictionary List
Given a list with all dictionary elements, the task is to write a Python program to extract keys having similar values across all dictionaries. Examples: Input : test_list = [{"Gfg": 5, "is" : 8, "best" : 0}, {"Gfg": 5, "is" : 1, "best" : 0}, {"Gfg": 5, "is" : 0, "best" : 0}] Output : ['Gfg', 'best'
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
Python program to check whether the values of a dictionary are in same order as in a list
Given a dictionary, test if values are in order with list values. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub_list = [4, 10, 11] Output : True Explanation : Values are 4, 10, 11, same as list order. Hence True is returned. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub
6 min read
Python program to group keys with similar values in a dictionary
Given a dictionary with values as a list. Group all the keys together with similar values. Input : test_dict = {"Gfg": [5, 6], "is": [8, 6, 9], "best": [10, 9], "for": [5, 2], "geeks": [19]} Output : [['Gfg', 'is', 'for'], ['is', 'Gfg', 'best'], ['best', 'is'], ['for', 'Gfg']] Explanation : Gfg has
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 | Find dictionary matching value in list
Finding a dictionary with a specific value in a list of dictionaries involves searching through the list and matching a key-value pair in each dictionary. For example, given the list of dictionaries [{âCourseâ: âC++â, âAuthorâ: âJerryâ}, {âCourseâ: âPythonâ, âAuthorâ: âMarkâ}], you may want to find
3 min read
Python Program to extract dictionaries with maximum number of keys
Given a list of dictionaries, the task is to write a Python program to extract a dictionary with a maximum number of keys. Input : test_list = [{'Gfg' : 1}, {'Gfg' : 1, 'is' : 5, 'best' : 4}, {'Gfg' : 2, 'best' : 9}] Output : {'Gfg' : 1, 'is' : 5, 'best' : 4} Explanation : Dictionary with max. lengt
7 min read