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)) `

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

 `# 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)) `

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.

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))`

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)

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`

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

 `# 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))`

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

 `# 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))`

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.