A Python dictionary stores key-value pairs. Sorting by value means ordering those pairs based on the value part, not the key. In Python 3.7 and later, you can build a new dictionary from sorted items and that dictionary keeps the sorted order when you iterate.
If you need sort by key instead, see the companion guide for key-based sorting. For dict fundamentals, see Python dictionary.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Quick answer: sort a dictionary by value
Use dict(sorted(my_dict.items(), key=lambda item: item[1])) to sort a dictionary by value. The key function tells sorted() to compare the value from each (key, value) pair.
scores = {"Alice": 88, "Bob": 92, "Carol": 75}
sorted_scores = dict(sorted(scores.items(), key=lambda item: item[1]))
print(sorted_scores)After you run this, values appear from lowest to highest: Carol (75), Alice (88), Bob (92).
Python sort dictionary by value quick reference
| Task | Use |
|---|---|
| Sort dictionary by value ascending | dict(sorted(d.items(), key=lambda item: item[1])) |
| Sort dictionary by value descending | dict(sorted(d.items(), key=lambda item: item[1], reverse=True)) |
| Sort and keep as list of tuples | sorted(d.items(), key=lambda item: item[1]) |
| Sort using itemgetter | dict(sorted(d.items(), key=itemgetter(1))) |
| Sort by value, then key | dict(sorted(d.items(), key=lambda item: (item[1], item[0]))) |
| Sort numeric values | Same sorted() approach |
| Sort string values | Same sorted() approach |
| Sort case-insensitively | key=lambda item: item[1].lower() |
| Sort nested dictionary by inner value | key=lambda item: item[1]["field"] |
| Old Python versions | OrderedDict(sorted(...)) |
Sort dictionary by value using sorted() and lambda
my_dict.items() returns key-value pairs. lambda item: item[1] tells sorted() to compare values. Wrap the result with dict() when you need a dictionary object.
In Python 3.7+, the new dictionary preserves insertion order, so iteration follows sorted value order.
inventory = {"apples": 12, "bananas": 3, "cherries": 8}
sorted_inventory = dict(sorted(inventory.items(), key=lambda item: item[1]))
print(sorted_inventory)
for name, count in sorted_inventory.items():
print(name, count)This is the main method for everyday value sorting. You do not need nested loops or manual key matching.
Sort dictionary by value in descending order
Add reverse=True when you want highest values first. This fits scores, counts, prices, ranks, and frequencies.
students = {"Alice": 88, "Bob": 92, "Carol": 75}
by_marks_desc = dict(sorted(students.items(), key=lambda item: item[1], reverse=True))
print(by_marks_desc)Bob (92) appears first, then Alice (88), then Carol (75).
Keep sorted dictionary as list of tuples
sorted(d.items(), key=lambda item: item[1]) returns a list of (key, value) tuples. Use this when you only need sorted output and do not need dictionary behavior.
prices = {"bread": 2.50, "milk": 1.20, "eggs": 3.00}
sorted_pairs = sorted(prices.items(), key=lambda item: item[1])
print(sorted_pairs)
print(type(sorted_pairs))Sometimes a list of tuples is clearer than calling the result a “sorted dictionary.” Convert to dict() only when your code expects mapping methods such as .get().
Sort dictionary by value using itemgetter()
operator.itemgetter(1) selects the value from each pair. It is an alternative to lambda item: item[1]:
from operator import itemgetter
scores = {"Alice": 88, "Bob": 92, "Carol": 75}
sorted_scores = dict(sorted(scores.items(), key=itemgetter(1)))
print(sorted_scores)Use lambda for beginner readability. Use itemgetter(1) when you prefer a compact standard-library helper.
Sort dictionary by value, then by key
When multiple keys share the same value, sort by a tuple of (value, key) for predictable tie-breaking:
products = {"zebra": 10, "apple": 10, "widget": 5}
sorted_products = dict(sorted(products.items(), key=lambda item: (item[1], item[0])))
print(sorted_products)widget (5) comes first. The two items with value 10 follow in key order: apple, then zebra. Python sorting is stable, so equal sort keys keep their original relative order unless you add another tie-breaker.
Sort dictionary by numeric values
When values are numbers, sorted() compares them numerically:
scores = {"team_a": 42, "team_b": 17, "team_c": 35}
ascending = dict(sorted(scores.items(), key=lambda item: item[1]))
descending = dict(sorted(scores.items(), key=lambda item: item[1], reverse=True))
print(ascending)
print(descending)Use reverse=True for leaderboards, rankings, and “top N” style output.
Sort dictionary by string values
String values sort alphabetically by default. Sorting is case-sensitive unless you normalize case:
employees = {"E102": "Zara", "E101": "amit", "E103": "Bob"}
by_name = dict(sorted(employees.items(), key=lambda item: item[1]))
print(by_name)Bob sorts before Zara, and uppercase letters sort before lowercase in default string order.
Sort dictionary by value case-insensitively
Compare lowercase values when case should not affect order:
employees = {"E102": "Zara", "E101": "amit", "E103": "Bob"}
case_insensitive = dict(sorted(employees.items(), key=lambda item: item[1].lower()))
print(case_insensitive)For stronger Unicode handling, use casefold() instead of lower().
Sort nested dictionary by value
When each value is another dictionary, point the key function at the inner field you care about:
students = {
"A": {"score": 90},
"B": {"score": 75},
"C": {"score": 90},
}
by_score = dict(sorted(students.items(), key=lambda item: item[1]["score"], reverse=True))
print(by_score)This sorts by inner score. It does not sort fields inside each nested dictionary unless you apply sorting at that level too.
Sort dictionary by value using dict.get
sorted(d, key=d.get) sorts keys based on their values, then you rebuild the dictionary:
items = {"Fruit": "Mango", "Vegetable": "Potato", "Car": "Swift", "Bike": "Hornet"}
sorted_keys = sorted(items, key=items.get)
rebuilt = {key: items[key] for key in sorted_keys}
print(rebuilt)This works, but sorting d.items() with a key function is more direct for most code.
OrderedDict for old Python versions
In Python 3.7+, normal dict preserves insertion order as guaranteed language behavior. In Python 3.6, CPython preserved order as an implementation detail. Use collections.OrderedDict only when you need older-version compatibility:
from collections import OrderedDict
scores = {"Alice": 88, "Bob": 92, "Carol": 75}
ordered = OrderedDict(sorted(scores.items(), key=lambda item: item[1]))
print(ordered)On modern Python, prefer dict(sorted(...)) unless compatibility requires OrderedDict.
Counter.most_common() for frequency dictionaries
Counter fits count or frequency dictionaries. most_common() returns items sorted by count, highest first:
from collections import Counter
counts = {"error": 12, "info": 45, "debug": 8}
for word, total in Counter(counts).most_common():
print(word, total)This is a special case, not a general replacement for sorting any dictionary by value. Use it when values represent counts.
Sort dictionary by value vs sort by key
| Task | Main expression |
|---|---|
| Sort by key | dict(sorted(d.items())) |
| Sort by value | dict(sorted(d.items(), key=lambda item: item[1])) |
| Sort by value descending | dict(sorted(d.items(), key=lambda item: item[1], reverse=True)) |
| Sort by value then key | dict(sorted(d.items(), key=lambda item: (item[1], item[0]))) |
See sort dictionary by key for key-first patterns and JSON or pprint display options.
Common mistakes to avoid
- Using
sorted(d)and expecting values to be sorted. That returns sorted keys only. - Forgetting
.items(). You need pairs when sorting by value. - Sorting by
item[0]when you meant value. Useitem[1]for values. - Using
zip(values, keys)and reversing key/value order by mistake. - Using
Counter.most_common()for non-count dictionaries. It is for frequency data. - Using pandas for a normal dictionary. The standard library is enough.
- Expecting
sorted()to modify the original dictionary. It returns a new list. - Forgetting
reverse=Truefor descending order. - Sorting values that mix incompatible types such as strings and integers without a custom
key. - Assuming duplicate values disappear. Keys stay unique; only order changes.
Summary
To sort a dictionary by value in Python, use dict(sorted(d.items(), key=lambda item: item[1])). Add reverse=True for descending order. Use itemgetter(1) as a lambda alternative, and use (item[1], item[0]) as the sort key when values tie. Keep results as a list of tuples when you only need sorted output. Use OrderedDict only for older Python compatibility, and use Counter.most_common() only for count dictionaries. For key-based sorting, see the companion article on sorting by key.
References
- Python
sorted()documentation - Python
operator.itemgetter - Python
collections.Counter - Python dictionary data structures

