When you are given a dictionary, you can do a lot of things with both its keys and its values. From time to time, you may need to do some sort of comparison based on the values of the dictionary. This can include finding the largest value, the smallest value, the sum of all the values, etc.
Let us assume that you are given a dictionary with the following keys and values:
grades = {“math”: 90, “history”: 80, “science”: 70}
Now you want to find the highest value in the dictionary, which in our case means that we want to find the subject in which a student has reached the highest scores.
To do that, we can use the max() function as shown below:
As you can see, we are using the additional _key_ parameter where we specify that we want to do the comparison based on the values of each item in the dictionary.
This can be something that you may not have seen before.
I hope you find it useful.