- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find Maximum value from dictionary whose key is present in the list
In python we are can use simple for loop and max, count and operator function to find the maximum value from the dictionary whose key is present in the list.
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.
Example
Assume we have taken an input dictionary and input list. We will now find the maximum value from an input dictionary whose key is also present in the input list.
Input
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15} inputList = ["python", "dictionaries", "article", 'codes']
Output
Maximum value of dictionary where the key is in the list: 15
In the above input dictionary, the maximum value is 20 whose corresponding key is tutorialspoint. But the word tutorialspoint is not present in the input list. So, the next maximum value is checked i.e, codes which is also in the list.
Hence, the output is 15.
Using for loop
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input dictionary.
Create another variable to store the input list.
Initialize a variable(maxValue) with 0 for storing the resultant maximum value of the dictionary.
Use the for loop to traverse through each element of the input list.
Use the if conditional statement to check whether the current element is present in the keys of a dictionary.
Use the max() function to get the maximum value from the above-initialized maxValue variable value and the current key value of the dictionary.
Print the resultant maximum value of an input dictionary where the key is present in the input list
Example
The following program returns the maximum value from an input dictionary whose key is also present in the input list using for loop and max() function –
# input dictionary inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15} # printing input dictionary print("Input dictionary:", inputDict) # input list inputList = ["python", "dictionaries", "article", 'codes'] # intializing with 0 for storing resultant max value maxValue = 0 # traversing through each element of the input list for e in inputList: # checking whether the current element is present in the keys of a dictionary if e in inputDict: # getting the max value from the maxValue variable and # the current key value of the dictionary maxValue = max(maxValue, inputDict[e]) # printing the resultant maximum value print("Maximum value of dictionary where the key is in the list:", maxValue)
Output
On executing, the above program will generate the following output –
Input dictionary: {'hello': 6, 'tutorialspoint': 20, 'python': 5, 'codes': 15} Maximum value of dictionary where the key is in the list: 15
Using list comprehension and max() function
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
max() method − returns the highest-valued item/greatest number in an iterable.
Example
The following program returns the maximum value from an input dictionary whose key is also present in the input list using list comprehension and the max() function –
# input dictionary inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15} # input list inputList = ["python", "dictionaries", "article", 'codes'] # Get all the elements Values that are present in both dictionary and the list # Get the max of this list using the max() function maxValue = max([inputDict[e] for e in inputList if e in inputDict]) # printing the resultant maximum value print("Maximum value of dictionary where the key is in the list:", maxValue)
Output
Maximum value of dictionary where the key is in the list: 15
Using the Counter() function
Counter() function − a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.
In this method, we use the Counter() function to get the frequency of list elements as a key-value pair.
Example
The following program returns the maximum value from an input dictionary whose key is also present in the input list using the Counter() function –
# importing a Counter function from the collections module from collections import Counter # input dictionary inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15} # input list inputList = ["python", "dictionaries", "article", 'codes'] # getting the frequency of input list elements as a key-value pair listFrequency = Counter(inputList) # intializing with 0 for storing resultant max value maxValue = 0 # traversing through each element of the input list for p in inputDict: # checking whether the current element is present in the keys # of above list frequency if p in listFrequency.keys(): # getting the max value from the maxValue variable # and the current key value of dictionary maxValue = max(maxValue, inputDict[p]) # printing the resultant maximum value print("Maximum value of dictionary where the key is in the list:", maxValue)
Output
Maximum value of dictionary where the key is in the list: 15
Using the operator.countOf() method
In this method, we are going to use the coutof() function of operator library in python to find the maximum value from the dictionary.
Syntax
operator.countOf(a, b)
The countOf() function of the operator module returns the number of elements in a that are equal to b.
Example
The following program returns the maximum value from an input dictionary whose key is also present in the input list using list comprehension and the operator.countOf() function –
import operator as op # input dictionary inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15} # input list inputList = ["python", "dictionaries", "article", 'codes'] maxValue = max([inputDict[e] for e in inputList if op.countOf(inputDict, e) > 0]) # printing the resultant maximum value print("Maximum value of dictionary where the key is in the list:", maxValue)
Output
Maximum value of dictionary where the key is in the list: 15
Conclusion
The maximum value from a dictionary whose key is included in the list can be found using one of four different techniques covered in this article. The new method was another thing we learned operator.countOf() function to determine the iterable's, element count
To Continue Learning Please Login
Login with Google