Python Program Example to Get Minimum and Maximum Value from Dictionary

Python Program Example to Get Minimum and Maximum Value from Dictionary

  • Python
  • 1 min read

Below is a Python program example to get minimum and maximum value from a Dictionary object.

Python: Get Minimum and Maximum Value from a Dictionary Example

In the following example, it will invert the keys and values of the dictionary object using the zip() method and will get the minimum and maximum values using the min() and max() methods. It also uses the sorted() to print the values of a dictionary in order.

prices = {
   'APPLE': 45.23,
   'ORANGE': 612.78,
   'MANGO': 205.55,
   'BLUEBERRY': 37.20,
   'STRAWBERRY': 10.75
}

# Find min and max price
min_price = min(zip(prices.values(), prices.keys()))
max_price = max(zip(prices.values(), prices.keys()))

print('min price:', min_price)
print('max price:', max_price)

print('sorted prices:')
prices_sorted = sorted(zip(prices.values(), prices.keys()))
for price, name in prices_sorted:
    print('    ', name, price)

Output

min price: (10.75, 'STRAWBERRY')
max price: (612.78, 'ORANGE')
sorted prices:
     STRAWBERRY 10.75
     BLUEBERRY 37.2
     APPLE 45.23
     MANGO 205.55
     ORANGE 612.78

Related tutorials:

Reference: Python Functions Manual