Last modified: Nov 14, 2026

Sort Python Dictionary by Value

Dictionaries are essential data structures in Python. They store data in key-value pairs. This makes data retrieval very fast. Sometimes, you need to organize this data. Sorting a dictionary by its values is a common task. This guide will show you how to do it easily.

Python dictionaries maintain insertion order since version 3.7. However, they do not sort automatically. You must use built-in functions to reorder them. We will explore the best methods to achieve this. We will cover ascending order, descending order, and handling ties.

Imagine you have a dictionary of student grades. You might want to see the highest grades first. Or maybe you have an inventory of products. You might want to sort them by price. These are practical scenarios where sorting is necessary.

Understanding the Sorting Process

To sort a dictionary, we first need to extract its contents. The items() method is perfect for this. It returns a view object containing key-value pairs as tuples. A tuple is an immutable sequence in Python.

Next, we use the sorted() function. This function takes an iterable and returns a sorted list. By default, it sorts by the first element of the tuple. For dictionaries, this first element is the key. We need to change this default behavior.

We can change the sorting behavior using the key parameter. This parameter accepts a function. This function tells Python what to sort by. We will use a lambda function to specify the value. A lambda function is a small anonymous function.

Understanding these building blocks is crucial. They form the foundation of dictionary manipulation. Once you grasp them, sorting becomes very intuitive.

Sorting in Ascending Order

Let us start with a basic example. We will sort a dictionary of fruit prices from lowest to highest. This is known as ascending order. It is the default sorting direction.


# Define a dictionary of fruits and their prices
fruit_prices = {
    "apple": 1.20,
    "banana": 0.50,
    "cherry": 2.50,
    "date": 3.00
}

# Sort the dictionary by value in ascending order
sorted_items = sorted(fruit_prices.items(), key=lambda item: item[1])

# Print the sorted list of tuples
print(sorted_items)

[('banana', 0.5), ('apple', 1.2), ('cherry', 2.5), ('date', 3.0)]

The output is a list of tuples. The lambda function lambda item: item[1] tells Python to sort by the second element. This second element is the dictionary value. The first element, item[0], would be the key.

Since the result is a list, you can easily check its size. You can check the size of your new list to verify the number of items. This is very useful when working with large datasets.

Sorting in Descending Order

Sometimes, you want the highest values first. This is called descending order. You can achieve this by adding the reverse parameter to the sorting function. This is highly useful for ranking systems.


# Define a dictionary of student scores
student_scores = {
    "Alice": 85,
    "Bob": 92,
    "Charlie": 78,
    "Diana": 95
}

# Sort by value in descending order
sorted_scores = sorted(student_scores.items(), key=lambda item: item[1], reverse=True)

# Print the sorted list
print(sorted_scores)

[('Diana', 95), ('Bob', 92), ('Alice', 85), ('Charlie', 78)]

Setting reverse=True flips the order. The highest scores now appear at the beginning of the list. This allows you to quickly identify the top performers. It is a simple but powerful feature.

Converting the List Back to a Dictionary

The sorting functions return a list of tuples. Often, you need a dictionary again. You can convert the list back using the dict() constructor. This wraps the list of tuples into a new dictionary.


# Define a dictionary of city temperatures
city_temps = {
    "New York": 55,
    "Los Angeles": 75,
    "Chicago": 45,
    "Miami": 85
}

# Sort by value and convert back to a dictionary
sorted_temp_list = sorted(city_temps.items(), key=lambda item: item[1])
sorted_temp_dict = dict(sorted_temp_list)

# Print the new dictionary
print(sorted_temp_dict)

{'Chicago': 45, 'New York': 55, 'Los Angeles': 75, 'Miami': 85}

The new dictionary maintains the sorted order. This works because modern Python dictionaries remember insertion order. If you want to explore more advanced dictionary creation, you might want to learn advanced dictionary creation techniques for cleaner code.

Finding Items in the Sorted List

Remember that the initial sorting result is a list. You might need to locate a specific item within this list. You can use list methods to locate specific elements within your sorted data. This is helpful for further data processing.


# Using the previous fruit prices example
fruit_prices = {"apple": 1.20, "banana": 0.50, "cherry": 2.50}
sorted_fruits = sorted(fruit_prices.items(), key=lambda item: item[1])

# Find the index of the cherry tuple
cherry_index = sorted_fruits.index(("cherry", 2.50))
print(f"Cherry is at index: {cherry_index}")

Cherry is at index: 2

This shows how sorted lists integrate with other list operations. It gives you full control over your sorted data. You can easily extract specific ranks or positions.

Using operator.itemgetter for Sorting

While lambda functions are great, there is another way. You can use the operator module. Specifically, the itemgetter function is very efficient. It is often faster than a lambda function for simple sorting tasks.


import operator

# Define a dictionary of book ratings
book_ratings = {
    "Book A": 4.5,
    "Book B": 3.8,
    "Book C": 4.9,
    "Book D": 4.1
}

# Sort using operator.itemgetter
sorted_books = sorted(book_ratings.items(), key=operator.itemgetter(1))

print(sorted_books)

[('Book B', 3.8), ('Book D', 4.1), ('Book A', 4.5), ('Book C', 4.9)]

The number inside itemgetter specifies the index. The number 1 means the second element. This is the dictionary value. This method is highly readable and considered very Pythonic.

Handling Ties in Values

What happens if two keys have the same value? Python needs a tie-breaker. You can sort by value first, and then by key. This requires a slightly more complex lambda function. It ensures a deterministic output.


# Dictionary with duplicate values
employee_salaries = {
    "John": 50000,
    "Jane": 60000,
    "Doe": 50000,
    "Smith": 70000
}

# Sort by value, then by key alphabetically
sorted_employees = sorted(
    employee_salaries.items(),
    key=lambda item: (item[1], item[0])
)

print(sorted_employees)

[('Doe', 50000), ('John', 50000), ('Jane', 60000), ('Smith', 70000)]

The lambda function returns a tuple (item[1], item[0]). Python sorts by the first element of this tuple. This is the value. If values are equal, it sorts by the second element. This is the key. This guarantees a consistent order every time you run the code.

Conclusion

Sorting a dictionary by value is a straightforward process in Python. You extract the items, use a lambda function to target the values, and apply the sorting logic. You can easily