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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-python-dictionaries-by-key-or-value
Sort Python Dictionary by Key or Value - Python - GeeksforGeeks
3. Using for loop with sorted(): This method sorts and displays dictionary values in ascending order using for loop with sorted() function.
Published   January 13, 2026
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - To sort a Python dictionary by its keys, you use the sorted() function combined with .items(). This approach returns a list of tuples sorted by keys, which you can convert back to a dictionary using the dict() constructor.
Discussions

Add a dict.sort() method - Ideas - Discussions on Python.org
Python dictionaries are now ordered ... a dictionary in place, you have to create a new one: from operator import itemgetter my_dict = {2: "a", 1: "b"} my_dict = dict(sorted(my_dict.items(), key=itemgetter(0))) It would be nice if they could be reordered in place like lists, with the same interface. The argument passed to the function passed in ... More on discuss.python.org
🌐 discuss.python.org
1
November 14, 2020
python - How do I sort a dictionary by value? - Stack Overflow
The first because it eases dispatch in the implementation of functions and methods in some cases. The second as it encourages to more easily use dicts as intermediate storage in processing pipelines. Raymond Hettinger kindly provided documentation explaining "The Tech Behind Python 3.6 Dictionaries... More on stackoverflow.com
🌐 stackoverflow.com
Sorted function in Python dictionary - Stack Overflow
I have a dictionary and I need to print the contents of dictionary first sorted by "value" if there is any value tie then I need to use "key" as a tie breaker so that the keys t... More on stackoverflow.com
🌐 stackoverflow.com
sorting with key=lambda
pair is a tuple consisting of two items. Index 0 is the first item, index 1 is the second item. There is no other item in the tuple for any other index value to be valid. More on reddit.com
🌐 r/learnpython
11
1
March 14, 2023
🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.4 documentation
February 23, 2026 - Author, Andrew Dalke and Raymond Hettinger,. Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted lis...
🌐
DataCamp
datacamp.com › tutorial › sort-a-dictionary-by-value-python
How to Sort a Dictionary by Values in Python | DataCamp
June 19, 2024 - To quickly sort a dictionary by value, we can use Python's built-in .sorted() function in combination with a lambda function.
🌐
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
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 in ascending or descending order.
🌐
Sentry
sentry.io › sentry answers › python › sort a dictionary by value in python
Sort a dictionary by value in Python | Sentry
This means that we can sort a dictionary by sorting its key-value pairs and creating a new dictionary from them. The code below shows how to do this: prices = {"Orange": 3, "Avocado": 5, "Apple": 1, "Pear": 2, "Grapefruit": 4} prices_sorted = dict(sorted(prices.items(), key=lambda item: item[1])) print(prices_sorted) # will output {"Apple": 1, "Pear": 2, "Orange": 3, "Grapefruit": 4, "Avocado": 5} Here we use Python’s built-in sorted() function to sort our dictionary’s items (i.e.
🌐
freeCodeCamp
freecodecamp.org › news › sort-dictionary-by-value-in-python
Sort Dictionary by Value in Python – How to Sort a Dict
September 13, 2022 - The sorted() method can accept up to 3 parameters: iterable – the data to iterate over. It could be a tuple, list, or dictionary. key – an optional value, the function that helps you to perform a custom sort operation.
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › sorting a dictionary in python
Sorting a Dictionary in Python | Towards Data Science
January 21, 2025 - We learned how the sorted() function works on any iterable object (without modifying it) and returns a new list, but the sort() method only works on lists and modifies them in-place and returns None. Lastly, we saw how we can use the sorted() function to sort dictionaries in python.
🌐
Python.org
discuss.python.org › ideas
Add a dict.sort() method - Ideas - Discussions on Python.org
November 14, 2020 - Python dictionaries are now ordered but there’s no way to reorder a dictionary in place, you have to create a new one: from operator import itemgetter my_dict = {2: "a", 1: "b"} my_dict = dict(sorted(my_dict.items(), ke…
Top answer
1 of 16
7096

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.

🌐
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 - To sort a dictionary by value in Python you can use the sorted() function. Python’s sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method.
🌐
Flexiple
flexiple.com › python › python-sort-dictionary-by-value
How to sort dictionary by value in Python? | Felxiple Tutorials | Python - Flexiple
March 23, 2022 - Here, we first use the sorted() function to order the values of the dictionary. The sorted() function does not re-order the dictionary in-place, hence, we store its value in sorted_values. We then loop through the sorted values, finding the keys for each value.
🌐
Tutorial Teacher
tutorialsteacher.com › articles › sort-dict-by-value-in-python
Sort a Dictionary by Value in Python
markdict = {"Tom":67, "Tina": 54, "Akbar": 87, "Kane": 43, "Divya":73} <pre className="language-python"><code>markdict = {"Tom":67, "Tina": 54, "Akbar": 87, "Kane": 43, "Divya":73} marklist = sorted(markdict.items(), key=lambda x:x[1]) sortdict = dict(marklist) print(sortdict) ... Another option is to use the itemgetter() function as defined in the operator module bundled with Python's standard library.
🌐
freeCodeCamp
freecodecamp.org › news › python-sort-dictionary-by-key
Python Sort Dictionary by Key – How to Sort a Dict with Keys
May 25, 2023 - Keys in a dictionary can be of any immutable data type, such as strings, numbers (integers or floats), or tuples. The key must be unique within the dictionary, meaning that no two keys can have the same value. The simplest way to sort a dictionary by its keys is by using the sorted() function along with the items() method of the dictionary.
🌐
Imperial College London
python.pages.doc.ic.ac.uk › 2022 › lessons › core10 › 05-lambda › 03-sorted.html
Lesson 10: I am Your Father > Lambda in sorted() | Python Programming (70053 Autumn Term 2022/2023) | Department of Computing | Imperial College London
You can use lambda function to sort the items by the values of the dictionary. This time, we will use the sorted() function rather than the mutable list.sort(). Just because we can! freq = {"python": 24, "cat": 78, "mat": 12, "aardvark": 1, "fish": 56} sorted_tuples = sorted(freq.items(), key=lambda x:x[1], reverse=True) print(sorted_tuples) ## [('cat', 78), ('fish', 56), ('python', 24), ('mat', 12), ('aardvark', 1)] Now, let’s say there are two words with the same frequency.
🌐
Trey Hunner
treyhunner.com › 2021 › 11 › how-to-sort-a-dictionary-in-python
How to sort a dictionary in Python
In the rare case that you care about the order of your dictionary’s items, keep in mind that dictionaries are ordered by the insertion order of their keys (as of Python 3.6). So the keys in your dictionary will remain in the order they were added to the dictionary. If you’d like to sort a dictionary by its keys, you can use the built-in sorted function along with the dict constructor:
🌐
YoungWonks
youngwonks.com › blog › python-sort-dictionary-by-key-or-value
What is a Python dictionary? How to create Python dictionary? Is there a way to sort a dictionary in Python?
April 10, 2023 - Sorted() is a built-in function in Python which takes an iterable object as an argument and returns a sorted list containing all of the elements of the iterable object. We can get sorted order of different data types using this function.
🌐
Medium
medium.com › pythons-gurus › sorting-a-python-dictionary-by-value-8d405bef3439
Sorting Python Dictionary By Value | Python’s Gurus
July 11, 2024 - The reverse parameter in the sorted() function determines the sorting order. reverse=True sorts in descending order (highest to lowest). reverse=False (or omitting the parameter) sorts in ascending order (lowest to highest). When dealing with dictionaries that have items with equal values, you might want to consider stable sorting. A stable sort maintains the relative order of items with equal values. Python’s sorted() function is guaranteed to be stable.
🌐
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 - 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.i...