To sort a Python dictionary by value, use the sorted() function with the items() method and a lambda function or operator.itemgetter() as the sorting key.

  • Ascending Order: sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item)) or sorted_dict = dict(sorted(my_dict.items(), key=operator.itemgetter(1))).

  • Descending Order: Add the reverse=True argument to the sorted() function.

  • Preserving Order: Use collections.OrderedDict if maintaining the sorted order in older Python versions is required, though standard dictionaries preserve insertion order in Python 3.6+.

Code Examples

import operator

my_dict = {'a': 2, 'b': 1, 'c': 3}

# Sort by value (Ascending)
sorted_dict_asc = dict(sorted(my_dict.items(), key=lambda item: item))
# Output: {'b': 1, 'a': 2, 'c': 3}

# Sort by value (Descending)
sorted_dict_desc = dict(sorted(my_dict.items(), key=lambda item: item, reverse=True))
# Output: {'c': 3, 'a': 2, 'b': 1}

# Using operator.itemgetter (Ascending)
sorted_dict_getter = dict(sorted(my_dict.items(), key=operator.itemgetter(1)))
# Output: {'b': 1, 'a': 2, 'c': 3}

Alternative Methods

  • collections.Counter: The most_common() method can sort a dictionary by values in descending order.

  • heapq: Use heapq.nlargest() or heapq.nsmallest() with operator.itemgetter(1) for large datasets.

  • numpy: Use np.argsort() for fast value-based sorting of numerical dictionaries.

Python 3.7+ or CPython 3.6

Dicts preserve insertion order in Python 3.7+. Same in CPython 3.6, but it's an implementation detail.

>>> x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
>>> {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

or

>>> dict(sorted(x.items(), key=lambda item: item[1]))
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

Older Python

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

For instance,

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))

sorted_x will be a list of tuples sorted by the second element in each tuple. dict(sorted_x) == x.

And for those wishing to sort on keys instead of values:

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))

In Python3 since unpacking is not allowed we can use

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=lambda kv: kv[1])

If you want the output as a dict, you can use collections.OrderedDict:

import collections

sorted_dict = collections.OrderedDict(sorted_x)
Answer from Devin Jeanpierre on Stack Overflow
Top answer
1 of 16
7095

Python 3.7+ or CPython 3.6

Dicts preserve insertion order in Python 3.7+. Same in CPython 3.6, but it's an implementation detail.

>>> x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
>>> {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

or

>>> dict(sorted(x.items(), key=lambda item: item[1]))
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

Older Python

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

For instance,

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))

sorted_x will be a list of tuples sorted by the second element in each tuple. dict(sorted_x) == x.

And for those wishing to sort on keys instead of values:

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))

In Python3 since unpacking is not allowed we can use

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=lambda kv: kv[1])

If you want the output as a dict, you can use collections.OrderedDict:

import collections

sorted_dict = collections.OrderedDict(sorted_x)
2 of 16
1649

As simple as: sorted(dict1, key=dict1.get)

Well, it is actually possible to do a "sort by dictionary values". Recently I had to do that in a Code Golf (Stack Overflow question Code golf: Word frequency chart). Abridged, the problem was of the kind: given a text, count how often each word is encountered and display a list of the top words, sorted by decreasing frequency.

If you construct a dictionary with the words as keys and the number of occurrences of each word as value, simplified here as:

from collections import defaultdict
d = defaultdict(int)
for w in text.split():
    d[w] += 1

then you can get a list of the words, ordered by frequency of use with sorted(d, key=d.get) - the sort iterates over the dictionary keys, using the number of word occurrences as a sort key.

for w in sorted(d, key=d.get, reverse=True):
    print(w, d[w])

or, if we want a dictionary back (since Python 3.6+ preserves insertion order):

{w: d[w] for w in sorted(d, key=d.get, reverse=True)}

I am writing this detailed explanation to illustrate what people often mean by "I can easily sort a dictionary by key, but how do I sort by value" - and I think the original post was trying to address such an issue. And the solution is to do sort of list of the keys, based on the values, as shown above.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-python-dictionaries-by-key-or-value
Sort Python Dictionary by Key or Value - Python - GeeksforGeeks
Dictionary comprehension rebuilds a new dictionary in value-sorted order. 1. Using sorted() with lambda: This method sorts the dictionary by its keys using sorted() and a lambda expression.
Published   January 13, 2026
Discussions

Sorting a dict by its values
Hi everyone I’ve been trying to come up with the most efficient way to sort a dictionary by its values but since there aren’t any sorting methods for a dictionary, I’ve been struggling l to do so. Any ideas? Thx More on discuss.python.org
🌐 discuss.python.org
14
0
October 3, 2023
How do you sort a dictionary by its values in Python?
There are a few ways to sort a dictionary by its values in Python. More on mindstick.com
🌐 mindstick.com
0
June 27, 2023
Most efficient way to sort the values (a list) of a dictionary?
There is no way to avoid iterating. But you just need to iterate over the values, which can then be sorted in place: for val in my_dict.values():          val.sort(key=lambda v: v[0]) More on reddit.com
🌐 r/learnpython
8
5
January 20, 2024
How do I sort dictionary keys by their values?
Please don't shadow builtin names like dict with your variables. sorted takes a key function to determine what to sort on, and a reverse argument to sort in reverse order. >>> votes.items() [('jane', 3), ('john', 6), ('jack', 4), ('jill', 0), ('joe', 2)] >>> sorted(votes.items(), key=lambda pair: pair[1], reverse=True) [('john', 6), ('jack', 4), ('jane', 3), ('joe', 2), ('jill', 0)] There is also a standard factory in operator for item getter functions like that: >>> import operator >>> second = operator.itemgetter(1) >>> sorted(votes.items(), key=second, reverse=True) [('john', 6), ('jack', 4), ('jane', 3), ('joe', 2), ('jill', 0)] For this particular case, the standard collections.Counter is most suitable both for collecting votes and sorting them: >>> from collections import Counter >>> Counter(votes).most_common() [('john', 6), ('jack', 4), ('jane', 3), ('joe', 2), ('jill', 0)] >>> votes = Counter() >>> votes['john'] += 1 >>> votes['john'] += 1 >>> votes['jill'] += 1 >>> votes.most_common() [('john', 2), ('jill', 1)] More on reddit.com
🌐 r/learnpython
10
19
July 14, 2014
🌐
SaltyCrane
saltycrane.com › blog › 2007 › 09 › how-to-sort-python-dictionary-by-keys
How to sort a Python dict (dictionary) by keys or values - SaltyCrane Blog
September 13, 2007 - mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':3} print ('Sort by keys:') for key in sorted(mydict.keys()): print ("%s: %s" % (key, mydict[key])) print ('Sort by items:') for key, value in sorted(mydict.items(), key=lambda item: (item[1], item[0])): print ("%s: %s" % (key, value)) ... It works perfect on 2.7. Thanks! ... Its worth noting that Python has a number of dictionary implementations that maintain the order based on the key.
🌐
Scaler
scaler.com › home › topics › sort dictionary by key in python
Sort Dictionary by Key in Python - Scaler Topics
February 11, 2022 - A dictionary can be sorted by both keys and values using the sorted() method. This article by Scaler topics covers the different ways of sorting a dictionary by key in Python.
🌐
iO Flood
ioflood.com › blog › python-sort-dictionary-by-value
Python Sort Dictionary by Value | Handling Data Structures
August 13, 2024 - Learn reliable methods to sort dictionary by value in Python in this guide with examples on `sorted()` , `operator.itemgetter()`, and lambda functions
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › sort dictionary by value in python
Efficient Dictionary Sorting in Python: Sort by Values with Ease
December 4, 2024 - By values: The focus of this tutorial, ... concern. To sort a dictionary by its keys in Python, you can use the sorted() function along with a dictionary comprehension....
Find elsewhere
🌐
TechBeamers
techbeamers.com › python-sort-dictionary-by-value
Sort Python Dictionary by Value - TechBeamers
November 30, 2025 - In this example, the key=lambda item: item[1] lambda function extracts the values for sorting, resulting in a dictionary sorted by its values in ascending order. The operator module in Python provides a convenient itemgetter() function for extracting values or elements from an iterable.
🌐
Board Infinity
boardinfinity.com › blog › sort-dictionary-in-python
Sort Dictionary in Python | Board Infinity
August 18, 2025 - To sort a dictionary by key, we can directly use the sorted method. In this method, the dictionary is first passed to the sorted() which returns the sorted dictionary. But, the sorted() method in Python returns a list of tuples that contains ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-dictionary-by-value-python-descending
Sort Dictionary by Value Python Descending - GeeksforGeeks
July 23, 2025 - In this approach, we use the sorted() method and Lambda function to sort the input_dict by values in the descending (high to low) order and store the result in the new dictionary as output with the descending order sorted key-value pairs.
🌐
freeCodeCamp
freecodecamp.org › news › sort-dictionary-by-value-in-python
Sort Dictionary by Value in Python – How to Sort a Dict
September 13, 2022 - However, I figured out a way to sort dictionaries by value, and that’s what I’m going to show you how to do in this article. ... The sorted() method sorts iterable data such as lists, tuples, and dictionaries.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python sort dictionary by key
Python Sort Dictionary by Key - Spark By {Examples}
May 31, 2024 - We can sort the dictionary by key using a sorted() function in Python. It can be used to sort dictionaries by key in ascending order or descending order.
🌐
Quora
quora.com › How-do-you-sort-the-dict-by-value-in-Python-Python-3-sorting-dictionary-and-development
How to sort the dict by value in Python (Python 3, sorting, dictionary, and development) - Quora
Sorting a dictionary by value in Python 3 is a common task; the recommended approach is to use the built-in sorted() with dict.items() and a key function, then convert the result to the container you need (list of tuples, OrderedDict, or dict ...
🌐
RS Blog
reneshbedre.com › blog › python-sort-dictionary.html
Sort dictionary by key and value in Python
March 26, 2022 - Here you will use the sorted() function to sort the dictionary by value
🌐
MindStick
mindstick.com › forum › 158872 › how-do-you-sort-a-dictionary-by-its-values-in-python
How do you sort a dictionary by its values in Python? – MindStick
June 27, 2023 - To sort a dictionary by its values, you would simply pass the dictionary to the sorted() function and specify the key argument. The key argument takes a function as its input, and the function will be used to determine the order of the elements ...
🌐
Codecademy
codecademy.com › article › how-to-sort-a-dictionary-by-key-or-value-in-python
How to Sort a Dictionary by Key or Value in Python | Codecademy
Python provides the built-in sorted() function to efficiently sort dictionaries. The sorted() function returns a new sorted list derived from the elements of any iterable. When applied to a dictionary’s items, it enables sorting by values ...
🌐
Edureka
edureka.co › blog › how-to-sort-a-dictionary-in-python
How To Sort A Dictionary In Python | Sort By Keys, Values | Edureka
November 27, 2024 - This blog explains how we can sort a dictionary in python with various approaches including sort by keys, sort by values, custom sorting algorithms etc.
🌐
Medium
medium.datadriveninvestor.com › how-to-sort-a-dictionary-by-key-and-value-in-python-6358a8142c73
How to sort a dictionary by key and value in Python? | by Hossen | DataDrivenInvestor
April 2, 2020 - It is not possible to sort a dictionary, but we can make a representation of a dictionary that is sorted. So You need an ordered data type to represent sorted keys or values, which will be a list — probably a list of tuples.
🌐
Educative
educative.io › answers › how-to-sort-a-dictionary-in-python
How to sort a dictionary in Python
A dictionary in Python is a data structure which stores values as a key-value pair. We can sort this type of data by either the key or the value and this is done by using the sorted() function.
🌐
Intellipaat
intellipaat.com › home › blog › how to sort a dictionary by value in python?
How to Sort a Dictionary by Value in Python? - Intellipaat
February 3, 2026 - Answer: You can use Python’s in-built sorting() function along with the lambda function to sort a dictionary by value in Python.