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.

🌐
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 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
🌐
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 - While Python dictionaries maintain insertion order since Python 3.7, you often need to sort them by value rather than by key.
🌐
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.
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - Interactive Quiz Sorting a Python Dictionary: Values, Keys, and More · Sort Python dictionaries by keys or values using sorted(), lambdas, and itemgetter.
🌐
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
Find elsewhere
🌐
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 ...
🌐
YouTube
youtube.com › watch
How To Sort A Dictionary By Value (Python Recipes) - YouTube
In this video I am going to be showing you how you can sort a dictionary by its value in Python.▶ Become job-ready with Python:https://www.indently.io▶ Follo...
Published   July 1, 2024
🌐
Reddit
reddit.com › r/learnpython › how do i sort dictionary keys by their values?
r/learnpython on Reddit: How do I sort dictionary keys by their values?
July 14, 2014 -

If I have a dictionary:

dict = {
        "john":6,
        "jill":0,
        "jack":4,
        "joe":2,
        "jane":3
        }

How would I sort the keys by their values, so I would end up with something like this?

print dict
{'john': 6, 'jack': 4, 'jane': 3, 'joe': 2, 'jill': 0}

I'm familiar with the sorted() method, but that only prints the keys and even then in the ascending order. While I can live with ascending order, descending would be much better - and I certainly need the values.

I'm making a program that calculates votes for people, so after sorting the keys I will then make a for loop which prints them into a more readable format, but I'm fairly certain I can do that part - just adding this info in case it's relevant.

Thanks!

🌐
DataCamp
datacamp.com › tutorial › sort-a-dictionary-by-value-python
How to Sort a Dictionary by Values in Python | DataCamp
June 19, 2024 - Sorting a dictionary by its values in Python is a common task that can be accomplished easily using the .sorted() function. Whether we need the data in ascending or descending order, understanding these techniques will make our data manipulation ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-python-dictionary-by-value
Sort Python Dictionary by Value - GeeksforGeeks
July 23, 2025 - In this article, we'll explore five different methods to sort a Python dictionary by its values, along with simple examples for each approach.
🌐
Scaler
scaler.com › home › topics › sort dictionary by key in python
Sort Dictionary by Key in Python - Scaler Topics
February 11, 2022 - Or maybe we want to sort it with our highest marks first. This is where sorting comes into the picture. In Python, we can sort a dictionary both by key (in this case, subject name) and by value (marks scored).
🌐
Scaler
scaler.com › home › topics › sort dictionary by value in python
Sort Dictionary by Value in Python - Scaler Topics
June 27, 2022 - The Main Logic Behind this approach is to create a list of tuples and then sort them using the sorted, operator module, and itemgetter() Function by using each tuple's second value as the key and then converting the sorted list of tuples into a new dictionary. Note: This method is very similar to sorting a dictionary in Python using the lambda function.
🌐
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.
🌐
Medium
medium.com › @python-javascript-php-html-css › sorting-a-list-of-dictionaries-in-python-by-a-specific-key-0e93fa7ffff1
Using a Specific Key to Sort a List of Dictionaries in Python | by Denis Bélanger 💎⚡✨ | Medium
August 24, 2024 - For example, if we have a list ... age, and finally by city. This can be achieved using the sorted() function with a key parameter that returns a tuple of values to sort by....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Sort a List of Dictionaries by the Value of the Specific Key in Python | note.nkmk.me
May 14, 2023 - Sort a list, string, tuple in Python (sort, sorted) As shown above, an error is raised if the specified key does not exist. # sorted(l, key=lambda x: x['Point']) # KeyError: 'Point' ... In such a case, use the get() method of dict, which returns the default value for non-existent keys. Get value from dictionary by key with get() in Python
🌐
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.
🌐
All About AI-ML
indhumathychelliah.com › 2021 › 06 › 29 › 5-different-ways-to-sort-python-dictionary
5 Different Ways to Sort Python Dictionary – All About AI-ML
January 2, 2022 - Let’s look at 5 different ways to sort a python dictionary in this article. ... sorted(d1.items()) → d1.items() will sort the dictionary based on keys and will return list of tuples containing key-value pair
🌐
Mouse Vs Python
papayawhip-oyster-325761.hostingersite.com › home › python 201: how to sort a dictionary by value
Python 201: How to sort a dictionary by value - Mouse Vs Python
January 31, 2020 - The other day I was asked if there was a way to sort a dictionary by value. If you use Python regularly, then you know that the dictionary data structure is by definition an unsorted mapping type. Some would define a dict as a hash table. Regardless, I needed a way to sort a nested […]
🌐
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.