Find the Maximum or Minimum Value in a Dictionary in Python

Finding the maximum or minimum value in a dictionary allows you to identify the highest or lowest value among the values stored in the dictionary. In this blog post, we will explore different methods to find the maximum or minimum value in a dictionary in Python.

Method 1: Using the max() and min() functions

# Method 1: Using the max() and min() functions
my_dict = {"a": 10, "b": 5, "c": 20, "d": 15}
max_value = max(my_dict.values())
min_value = min(my_dict.values())

print("Maximum Value:", max_value)
print("Minimum Value:", min_value)

Output:

Maximum Value: 20
Minimum Value: 5

Method 2: Using the key parameter with the max() and min() functions

# Method 2: Using the key parameter with the max() and min() functions
my_dict = {"a": 10, "b": 5, "c": 20, "d": 15}
max_key = max(my_dict, key=my_dict.get)
min_key = min(my_dict, key=my_dict.get)

print("Key with Maximum Value:", max_key)
print("Key with Minimum Value:", min_key)

Output:

Key with Maximum Value: c
Key with Minimum Value: b