Recently, while working on a data analysis project, I needed to find which U.S. state had the highest sales from a large dataset stored in a Python dictionary. I quickly realized that although this is a common task, many beginners find it tricky when dictionaries get large or nested.
As someone who has been using Python for over a decade, I’ve often had to extract maximum or minimum values from dictionaries, whether for analyzing sales, tracking performance metrics, or processing API data.
So, in this tutorial, I’ll walk you through different ways to find the maximum value in a Python dictionary, from the simplest built-in methods to more advanced approaches.
Understand Python Dictionaries
Before we start finding maximum values, let’s quickly revisit what a Python dictionary is.
A Python dictionary is a collection of key-value pairs. Each key must be unique, and it maps to a corresponding value. Dictionaries are great for storing structured data, for example, sales figures by state, product prices, or user scores.
Here’s a quick example:
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}In this dictionary, the keys are U.S. states, and the values represent sales in dollars. Now, let’s explore how to find which state had the maximum sales.
Method 1 – Use Python’s Built-in max() Function
The easiest and most Pythonic way to find the maximum value in a dictionary is by using the built-in max() function.
The max() function can take a dictionary and an key argument to determine how the comparison should be made.
Here’s how I use it in my projects:
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}
max_state = max(sales_data, key=sales_data.get)
print("State with maximum sales:", max_state)
print("Maximum sales value:", sales_data[max_state])Output:
State with maximum sales: Texas
Maximum sales value: 92000You can see the output in the screenshot below.

This method is clean, simple, and efficient. The key=sales_data.get part tells Python to compare the dictionary values, not the keys.
I often use this approach when I need to quickly identify the top-performing category or region in a dataset.
Method 2 – Use a For Loop (Manual Comparison)
Sometimes, I prefer to use a manual approach, especially when teaching beginners or debugging code.
This method uses a simple for loop to iterate through the dictionary and keep track of the maximum value and its corresponding key.
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}
max_key = None
max_value = float('-inf')
for state, sales in sales_data.items():
if sales > max_value:
max_value = sales
max_key = state
print("State with maximum sales:", max_key)
print("Maximum sales value:", max_value)Output:
State with maximum sales: Texas
Maximum sales value: 92000You can see the output in the screenshot below.

This approach gives you more control over the logic, and you can easily add conditions, for example, ignoring states with missing data or zero sales.
I often rely on this method when working with more complex data structures or when I need to apply additional filters.
Method 3 – Use the operator Module
If you want to make your code even more readable, Python’s operator module is a great choice.
The operator.itemgetter() function allows you to extract the maximum value directly from the dictionary’s items.
import operator
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}
max_state, max_value = max(sales_data.items(), key=operator.itemgetter(1))
print("State with maximum sales:", max_state)
print("Maximum sales value:", max_value)Output:
State with maximum sales: Texas
Maximum sales value: 92000You can see the output in the screenshot below.

This method is clean and efficient, especially when working with larger datasets. It’s also useful when sorting or ranking dictionary items based on their values.
Method 4 – Use Lambda Functions
If you prefer a more functional programming style, you can use a lambda function with max().
This is similar to the first method but gives you flexibility to apply custom logic.
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}
max_state = max(sales_data, key=lambda k: sales_data[k])
print("State with maximum sales:", max_state)
print("Maximum sales value:", sales_data[max_state])Output:
State with maximum sales: Texas
Maximum sales value: 92000You can see the output in the screenshot below.

I often use this approach when I need to compare dictionary values after applying transformations, like converting currencies or adjusting for inflation.
Method 5 – Find the Maximum Value in a Nested Python Dictionary
In real-world scenarios, data isn’t always flat. Sometimes, each key in your dictionary may contain another dictionary, for example, monthly sales data for each state.
Here’s an example of how I handle nested dictionaries in Python:
sales_data = {
'California': {'Q1': 21000, 'Q2': 22000, 'Q3': 20000, 'Q4': 23000},
'Texas': {'Q1': 24000, 'Q2': 25000, 'Q3': 26000, 'Q4': 28000},
'New York': {'Q1': 19000, 'Q2': 20000, 'Q3': 21000, 'Q4': 22000}
}
state_max = {}
for state, quarters in sales_data.items():
state_max[state] = max(quarters.values())
overall_max_state = max(state_max, key=state_max.get)
print("State with highest quarterly sales:", overall_max_state)
print("Maximum quarterly sales value:", state_max[overall_max_state])Output:
State with highest quarterly sales: Texas
Maximum quarterly sales value: 28000This approach is especially useful when analyzing hierarchical data, such as regional sales, student grades, or departmental budgets.
Method 6 – Use sorted() to Get the Top N Values
Sometimes, you don’t just want the single highest value; you want the top N results.
Python’s sorted() function makes this easy.
sales_data = {
'California': 85000,
'Texas': 92000,
'New York': 78000,
'Florida': 64000,
'Illinois': 71000
}
top_states = sorted(sales_data.items(), key=lambda x: x[1], reverse=True)[:3]
print("Top 3 states by sales:")
for state, sales in top_states:
print(f"{state}: ${sales}")Output:
Top 3 states by sales:
Texas: $92000
California: $85000
New York: $78000This is one of my favorite techniques when I need to generate quick reports or rankings from a dictionary.
Common Mistakes to Avoid
Over the years, I’ve seen beginners make a few common mistakes when finding maximum values in Python dictionaries:
- Forgetting to use key=dictionary.get in the max() function.
- Confusing dictionary keys and values.
- Trying to find the maximum in an empty dictionary (which raises a ValueError).
- Not handling nested or missing data properly.
Always make sure your dictionary isn’t empty before calling max(). You can handle it like this:
if sales_data:
max_state = max(sales_data, key=sales_data.get)
else:
print("Dictionary is empty.")Real-World Use Case: Find Top Products by Revenue
Let’s look at a realistic business example. Suppose you’re analyzing e-commerce data to find which product generated the most revenue.
product_sales = {
'Laptop': 125000,
'Smartphone': 189000,
'Tablet': 97000,
'Headphones': 56000,
'Smartwatch': 72000
}
top_product = max(product_sales, key=product_sales.get)
print(f"The top-selling product in the U.S. is '{top_product}' with revenue of ${product_sales[top_product]:,}.")Output:
The top-selling product in the U.S. is 'Smartphone' with revenue of $189,000.This kind of analysis is something I regularly perform in Python when working with sales data, marketing metrics, or performance dashboards.
So, that’s how I find the maximum value in a Python dictionary.
The max() function is the simplest and Pythonic approach, but depending on your use case, you might prefer loops, lambda functions, or the operator module.
When working with nested dictionaries or when you need the top N values, Python’s flexibility really shines.
You may also like to read:
- Remove Item from Set in Python
- Check if a Python Set is Empty
- Add Item to Set in Python
- Create an Empty Set in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.