Python
Dictionary
Programming
Coding
Data Structures

How do you sort a dictionary by value?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, dictionaries are keyed collections, so they are not “sorted” by value automatically. What you usually do instead is sort the dictionary’s items and then choose the result shape you want: a list of key-value pairs, or a new dictionary whose insertion order reflects the sorted order. The distinction matters because sorting produces a new ordering; it does not reorder a dictionary in place.

Start with sorted(d.items(), key=...)

The standard solution is to sort the dictionary’s item pairs by the second element in each pair.

python
1scores = {"alice": 88, "bob": 75, "carol": 92}
2ordered_items = sorted(scores.items(), key=lambda item: item[1])
3
4print(ordered_items)

This returns:

python
[("bob", 75), ("alice", 88), ("carol", 92)]

d.items() gives tuples of the form (key, value), and item[1] means “sort using the value”.

This is the most important idea in the whole problem. Once you understand that, the rest is mostly about output format.

Convert Back to a Dictionary When Needed

In modern Python, dictionaries preserve insertion order. That means you can build a new dictionary from the sorted item list and keep the value-based ordering.

python
1scores = {"alice": 88, "bob": 75, "carol": 92}
2sorted_dict = dict(sorted(scores.items(), key=lambda item: item[1]))
3
4print(sorted_dict)

This prints a dictionary whose iteration order follows the sorted values.

Use this when you want a dictionary-shaped result. Use the list of tuples when you need an explicitly ordered sequence for display, ranking, or further transformation.

Sort in Descending Order with reverse=True

If you want highest values first, add reverse=True.

python
1scores = {"alice": 88, "bob": 75, "carol": 92}
2ranking = sorted(scores.items(), key=lambda item: item[1], reverse=True)
3
4print(ranking)

That is often more useful in reporting code because “sorted by value” often really means “largest values first”.

Ties Stay Stable

Python’s sorting is stable. If two items have the same value, their relative order remains the same as it was in the input iteration order.

python
scores = {"alice": 88, "bob": 88, "carol": 75}
ordered = sorted(scores.items(), key=lambda item: item[1])
print(ordered)

This is useful when equal values should keep a predictable order. If you want a secondary rule, add it explicitly.

python
ordered = sorted(scores.items(), key=lambda item: (item[1], item[0]))
print(ordered)

That sorts first by value, then by key.

operator.itemgetter Is a Nice Alternative

Many Python developers prefer operator.itemgetter(1) because it is a bit more explicit than a lambda for tuple indexing.

python
1from operator import itemgetter
2
3scores = {"alice": 88, "bob": 75, "carol": 92}
4ordered = sorted(scores.items(), key=itemgetter(1))
5
6print(ordered)

This does the same thing as the lambda version. The choice is mostly about readability preference.

Know What You Are Sorting

A common confusion is assuming the dictionary itself becomes permanently “sorted by value”. It does not. You created a new ordering based on the current values. If the values change later, you need to sort again.

That is why it is often better to think of sorting as a view or transformation of the data rather than a permanent property of the mapping.

Also note that sorting costs O(n log n) time because that is the complexity of comparison sorting over n items. For small and medium dictionaries, that is usually fine.

Common Pitfalls

  • Expecting a dictionary to sort itself in place by value.
  • Forgetting to sort d.items() and trying to sort the dictionary object directly by value.
  • Confusing ascending and descending order and forgetting reverse=True.
  • Losing ordering intent by converting to a regular dictionary in older Python versions where insertion order was not guaranteed.
  • Assuming ties will use a secondary key automatically when you did not specify one.

Summary

  • In Python, sort a dictionary by value with sorted(d.items(), key=lambda item: item[1]).
  • The direct result is a list of (key, value) pairs.
  • Convert back to dict(...) if you want a dictionary-shaped result with sorted insertion order.
  • Use reverse=True for descending order.
  • Add a secondary sort key explicitly when ties need a deterministic rule.

Course illustration
Course illustration

All Rights Reserved.