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
7099

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
1650

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.

🌐
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. But it sorts by key only. The sorted() method puts the sorted items in a list. That’s another problem we have to solve, because we want the sorted dictionary to remain a dictionary. For instance, sorted() arranged the list below in alphabetical order:
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 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
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 to sort python dictionary?
Please format your code for Reddit. Especially Python code. More on reddit.com
🌐 r/learnprogramming
7
2
February 27, 2023
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - More examples and explanations of the key parameter will come later in the tutorial when you use it to sort dictionaries by values or nested elements. If you take another look at the results of this last sorting, you may notice the stability of the sorted() function. The three elements, aa, ba and ca, are equivalent when sorted by their second character. Because they’re equal, the sorted() function conserves their original order. Python ...
🌐
Flexiple
flexiple.com › python › python-sort-dictionary-by-value
How to sort dictionary by value in Python? | Felxiple Tutorials | Python - Flexiple
March 23, 2022 - By using a for loop along with the sorted() function in Python, we can sort the dictionary by value. Here is an example for the same.
🌐
Medium
medium.com › pythons-gurus › sorting-a-python-dictionary-by-value-8d405bef3439
Sorting Python Dictionary By Value | Python’s Gurus
July 11, 2024 - The most straightforward way to sort a dictionary by its values in Python involves using the sorted() function along with a custom key function. Here's how we can do it: # Our example dictionary of word frequencies word_freq = { 'the': 2, 'quick': ...
🌐
DataCamp
datacamp.com › tutorial › sort-a-dictionary-by-value-python
How to Sort a Dictionary by Values in Python | DataCamp
June 19, 2024 - A1: Yes, we can sort a dictionary by values, even if the values are strings. The .sorted() function will sort the values alphabetically in ascending or descending order, just as it does with numbers.
🌐
Programiz
programiz.com › python-programming › examples › sort-dictionary-value
Python Program to Sort a Dictionary by Value
To understand this example, you should have the knowledge of the following Python programming topics: ... dt = {5:4, 1:6, 6:3} sorted_dt = {key: value for key, value in sorted(dt.items(), key=lambda item: item[1])} print(sorted_dt)
Find elsewhere
🌐
Vultr
docs.vultr.com › python › examples › sort-a-dictionary-by-value
Python Program to Sort a Dictionary by Value | Vultr Docs
November 21, 2024 - Use the sorted() function along with a lambda function to sort the dictionary. ... scores = {'Alice': 58, 'Bob': 75, 'Charlie': 44, 'David': 89} sorted_scores = sorted(scores.items(), key=lambda x: x[1]) print(sorted_scores) Explain Code · ...
🌐
OneUptime
oneuptime.com › home › blog › how to sort a dictionary by value in python
How to Sort a Dictionary by Value in Python
January 25, 2026 - inventory = { 'laptop': {'price': 999, 'stock': 50}, 'mouse': {'price': 29, 'stock': 200}, 'keyboard': {'price': 79, 'stock': 150}, 'monitor': {'price': 299, 'stock': 75} } # Sort by price (cheapest first) by_price = dict(sorted(inventory.items(), ...
🌐
Stack Abuse
stackabuse.com › how-to-sort-dictionary-by-value-in-python
How to Sort a Dictionary by Value in Python
September 23, 2022 - In this article, we'll explore how to sort a dictionary in Python by its value. These solutions use for loops and the sorted() function, as well as lambdas and the operator module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-python-dictionaries-by-key-or-value
Sort Python Dictionary by Key or Value - Python - GeeksforGeeks
Let's explore different methods to sort dictionary by key or value in Python. 1. Using sorted() with lambda: This method sorts the dictionary efficiently by its values using the sorted() function and a lambda expression.
Published   January 13, 2026
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-python-dictionary-by-value
Sort Python Dictionary by Value - GeeksforGeeks
July 23, 2025 - Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, along with simple examples for each approach.
🌐
Tutorial Teacher
tutorialsteacher.com › articles › sort-dict-by-value-in-python
Sort a Dictionary by Value in Python
import operator <pre className="language-python"><code>import operator markdict = {"Tom":67, "Tina": 54, "Akbar": 87, "Kane": 43, "Divya":73} marklist= sorted(markdict.items(), key=operator.itemgetter(1)) sortdict=dict(marklist) print(sortdict) ...
🌐
AskPython
askpython.com › home › 3 ways to sort a dictionary by value in python
3 Ways to Sort a Dictionary by Value in Python - AskPython
August 6, 2022 - The operator.itemgetter(m) method considers the input object as an iterable and fetches all the ‘m’ values from the iterable. ... Python sorted() method sorts the dict in an ascending/descending order. ... from operator import itemgetter inp_dict = { 'a':3,'ab':2,'abc':1,'abcd':0 } print("Dictionary: ", inp_dict) sort_dict= dict(sorted(inp_dict.items(), key=operator.itemgetter(1))) print("Sorted Dictionary by value: ", sort_dict)
🌐
Python.org
discuss.python.org › python help
Sorting a dict by its values - Python Help - Discussions on Python.org
October 3, 2023 - 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
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › sort dictionary by value in python
Sort Dictionary by Value in Python
April 20, 2026 - To sort dictionary by value in Python, you can use the sorted() function with a custom sorting key. Here is an example:
🌐
Career Karma
careerkarma.com › blog › python › how to sort a dictionary by value in python
How to Sort a Dictionary by Value in Python | Career Karma
December 1, 2023 - Using the Python sorted() method, you can sort the contents of a dictionary by value. For instance, to rank the popularity of items on a coffee menu, or list those items in alphabetical order, you can use Python’s sorted() method.
🌐
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
🌐
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.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to sort a dictionary by value in python?
How To Sort A Dictionary By Value in Python? - Be on the Right Side of Change
October 18, 2020 - Summary: Use one of the following methods to sort a dictionary by value: Using The sorted(dict1, key=dict1.get) Method. Using Dictionary Comprehension And Lambda With sorted() Method.