Sort Dictionary by Value in Python

Learn how to sort a dictionary by value in Python using sorted(), lambda, itemgetter(), reverse=True, and custom sorting. See examples for ascending order, descending order, duplicate values, and nested dictionaries.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Sort Dictionary by Value in Python

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.

python
scores = {"Alice": 88, "Bob": 92, "Carol": 75}
sorted_scores = dict(sorted(scores.items(), key=lambda item: item[1]))
print(sorted_scores)
Output

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.

python
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)
Output

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.

python
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)
Output

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.

python
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))
Output

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]:

python
from operator import itemgetter

scores = {"Alice": 88, "Bob": 92, "Carol": 75}
sorted_scores = dict(sorted(scores.items(), key=itemgetter(1)))
print(sorted_scores)
Output

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:

python
products = {"zebra": 10, "apple": 10, "widget": 5}

sorted_products = dict(sorted(products.items(), key=lambda item: (item[1], item[0])))
print(sorted_products)
Output

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:

python
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)
Output

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:

python
employees = {"E102": "Zara", "E101": "amit", "E103": "Bob"}

by_name = dict(sorted(employees.items(), key=lambda item: item[1]))
print(by_name)
Output

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:

python
employees = {"E102": "Zara", "E101": "amit", "E103": "Bob"}

case_insensitive = dict(sorted(employees.items(), key=lambda item: item[1].lower()))
print(case_insensitive)
Output

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:

python
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)
Output

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:

python
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)
Output

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:

python
from collections import OrderedDict

scores = {"Alice": 88, "Bob": 92, "Carol": 75}
ordered = OrderedDict(sorted(scores.items(), key=lambda item: item[1]))
print(ordered)
Output

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:

python
from collections import Counter

counts = {"error": 12, "info": 45, "debug": 8}

for word, total in Counter(counts).most_common():
    print(word, total)
Output

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. Use item[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=True for 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


Frequently Asked Questions

1. How do you sort a dictionary by value in Python?

Use dict(sorted(my_dict.items(), key=lambda item: item[1])). The key function tells sorted() to compare values instead of keys.

2. How do you sort a dictionary by value in descending order?

Add reverse=True: dict(sorted(my_dict.items(), key=lambda item: item[1], reverse=True)).

3. What is the difference between sorting by key and sorting by value?

Sort by key with dict(sorted(d.items())). Sort by value with dict(sorted(d.items(), key=lambda item: item[1])).

4. Does sorted() change the original dictionary?

No. sorted() returns a new list. Wrap the result with dict() when you need a new dictionary object.

5. How do you handle duplicate values when sorting a dictionary?

Dictionary keys stay unique. Use key=lambda item: (item[1], item[0]) to break ties by key when two values are equal.

6. When should you use Counter.most_common() instead of sorted()?

Use Counter.most_common() for frequency or count dictionaries when you want items ordered by count, usually highest first.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …