w3resource

Python Exercise: Get the maximum and minimum value in a dictionary

Python dictionary: Exercise-15 with Solution

Write a Python program to get the maximum and minimum value in a dictionary.

Sample Solution:-

Python Code:

my_dict = {'x':500, 'y':5874, 'z': 560}

key_max = max(my_dict.keys(), key=(lambda k: my_dict[k]))
key_min = min(my_dict.keys(), key=(lambda k: my_dict[k]))

print('Maximum Value: ',my_dict[key_max])
print('Minimum Value: ',my_dict[key_min])

Sample Output:

Maximum Value:  5874                                                                                          
Minimum Value:  500

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sort a dictionary by key.
Next:Write a Python program to get a dictionary from an object's fields.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts