# How to Find Max Value in Dictionary

## Problem Formulation and Solution Overview

In this article, you’ll learn how to find the maximum value in a Python dictionary.

To make it more fun, we have the following running scenario:

You are in the market for a car and have narrowed the search to a few possibilities. Your final choice will be based on the vehicle possessing the maximum MPG. A list of these cars was provided to you in a Dictionary key:value format.

💬 Question: How would we write Python code to determine the maximum value in a Dictionary?

We can accomplish this task by one of the following options:

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

` import operator`

## Method 1: Use dict.values() and max() to display max value

Use `dict.values()` and `max()` to determine and return the maximum value in a Dictionary using `max(dict.values())`.

```cars_dict = {'Malibu': 18, 'Skylark': 16, 'Satellite': 19,
'Torino': 27, 'Challenge': 20}
car_values = cars_dict.values()
max_value = max(car_values)
print(max_value)```

This code declares a Dictionary containing five (5) key:value pairs of `cars` and associated `MPG`. The values from each pair are extracted and saved to `car_values`.

The `max()` function is called and passed `car_values` to determine and return the maximum value. The result saves to `max_value` and is output to the terminal.

Output

## Method 2: Use list and max() to display max value

Use List and `max()` to determine and return the maximum value with `max(list(cars_dict.values()))`.

Here’s an example:

```cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenge': 20}
max_val = max(list(cars_dict.values()))
print(max_val)  ```

This code converts a Dictionary into a List, extracting values from the key:value pairs and `max()` to determine and return the maximum value.
The result saves to `max_value` and is output to the terminal.

Output

## Method 3: Use dict.keys() and dict.values() to display max value and key

This example uses `dict.keys()` and `dict.values()` to determine and return the maximum value and associated key.

```cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenger': 20}
sort_dict = sorted(cars_dict.items(), key=operator.itemgetter(1), reverse=True)
print(f'{sort_dict}:\t{sort_dict}')
```

This code declares a Dictionary containing five (5) key:value pairs of `cars` and associated `MPG`.

Then `dict.items()` and `operator.itemgetter()` sort the Dictionary key: value pairs in descending order. The maximum key:value pair is set as the first element.

The formatted result is output to the terminal.

Output

## Method 4: Use dict.get and max() to display max value and key

Use `dict.get` and `max()` to determine and return the maximum value and associated key with `max(dict, key=cars_dict.get)`.

```cars_dict = {'Malibu': 18, 'Skylark': 16,
'Satellite': 19, 'Torino': 27, 'Challenger': 20}
max_val = max(cars_dict, key=cars_dict.get)
print(max_val, cars_dict[max_val])```

This code declares a Dictionary containing five (5) key:value pairs of `cars` and associated `MPG`.

Then `max` is called passing the dictionary and retrieving the key:value pairs, which save to `max_val`.

The `print` statement determines the maximum value and outputs the appropriate key:value pair to the terminal.

Output

## Summary

These four (4) methods of finding the maximum value in a Dictionary should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!