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
1648

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
0
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
🌐
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.
🌐
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
🌐
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 ...
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › sort-a-dictionary-by-value-python
How to Sort a Dictionary by Values in Python | DataCamp
June 19, 2024 - The .sorted() function is a powerful and flexible tool for sorting in Python. To sort a dictionary by its values, we can use a lambda function to specify that the sorting should be based on the dictionary’s values.
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - When using the .items() method on a dictionary and feeding it into the sorted() function, you’re passing in an iterable of tuples, and the sorted() function compares the entire tuple directly.
🌐
Upgrad
upgrad.com › home › blog › data science › sort dictionary by value python
Sort Dictionary by Value Python
November 25, 2025 - Python tutorial concepts in 2024. A for Loop can be used to sort a dictionary by value in Python. The values of the dictionary are ordered with the sorted() function. The compiler then loops through the sorted values to determine the keys for ...
🌐
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.
🌐
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 - Often you only need the highest or lowest values. scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78, 'Diana': 95, 'Eve': 88} # Top 3 scores top_3 = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True)[:3]) print(top_3) # {'Diana': 95, 'Bob': 92, 'Eve': 88} # Bottom 3 scores bottom_3 = dict(sorted(scores.items(), key=lambda x: x[1])[:3]) print(bottom_3) # {'Charlie': 78, 'Alice': 85, 'Eve': 88} For large dictionaries, heapq.nlargest and heapq.nsmallest are more efficient when you only need a few items.
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › most efficient way to sort the values (a list) of a dictionary?
r/learnpython on Reddit: Most efficient way to sort the values (a list) of a dictionary?
January 20, 2024 -

NOT to sort a dictionary by value.

The values of my dictionary are lists (specifically, a list of (x,y) tuples). I want for each key, the list to be sorted by the first element in each tuple. I want to avoid the simple solution of iterating over each key-value if possible.

For example, the output should be A: [(1,2), (2,5), (3,3), (4,2), (5,9)], B: [...]

🌐
Sentry
sentry.io › sentry answers › python › sort a dictionary by value in python
Sort a dictionary by value in Python | Sentry
As items will be inserted into our new dictionary in order of their values, we now have a dictionary ordered by value. In older versions of Python, we would need to use an OrderedDict from Python’s built-in collections module in place of an ordinary dictionary, as shown below: from collections import OrderedDict prices = {"Orange": 3, "Avocado": 5, "Apple": 1, "Pear": 2, "Grapefruit": 4} prices_sorted = OrderedDict(sorted(prices.items(), key=lambda item: item[1])) print(prices_sorted) # will output OrderedDict([("Apple", 1), ("Pear", 2), ("Orange", 3), ("Grapefruit", 4), ("Avocado", 5)])
🌐
Python Engineer
python-engineer.com › posts › sort-dictionary-values
How to sort a dictionary by values in Python - Python Engineer
Then you need to convert it back either with dictionary comprehension or simply with the dict() function: sorted_data = {k: v for k, v in sorted(data.items(), key=lambda x: x[1])} print(sorted_data) # {'d': 0, 'e': 1, 'c': 3, 'a': 4, 'b': 99} # Or sorted_data = dict(sorted(data.items(), key=lambda x: x[1])) print(sorted_data) # {'d': 0, 'e': 1, 'c': 3, 'a': 4, 'b': 99} Explanation: data.items() returns both the keys and the values as tuple.
🌐
Trey Hunner
treyhunner.com › 2021 › 11 › how-to-sort-a-dictionary-in-python
How to sort a dictionary in Python
If we want to sort the dictionary by its values, we could make a key function that accepts each item in our list of 2-item tuples and returns just the value: Then we’d use our key function by passing it to the sorted function (yes functions can be passed to other functions in Python) and pass the result to dict to create a new dictionary:
🌐
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!