The sorted() function takes a key= parameter

newlist = sorted(list_to_be_sorted, key=lambda d: d['name'])

Alternatively, you can use operator.itemgetter instead of defining the function yourself

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name'))

For completeness, add reverse=True to sort in descending order

newlist = sorted(list_to_be_sorted, key=itemgetter('name'), reverse=True)
Answer from Mario F on Stack Overflow
🌐
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
Discussions

How to sort a list containing dictionary items?
List objects have a sort function to sort the list in-place. This function has a key parameter to specify a function that will be called on each item in the list, after which the items will be sorted based on the results of that function. It also accepts a reverse parameter that, if set to True, will cause sort to sort the list in descending order. Let's say you have a list students that contains Student objects. To sort the students by age from oldest to youngest, you'd do students.sort(key = lambda x: x.Age, reverse=True). Sorting your list of dictionaries is done in pretty much the same way. See this if you need more examples. Keep in mind that in your list, the currency values are strings, not integers. You'll need to convert them first, otherwise they won't be sorted correctly. You can do that in the lambda function you pass to sort. Incidentally, using variable names like list is bad practice. You're overriding the built-in list class by doing that. More on reddit.com
🌐 r/learnpython
4
2
September 30, 2023
python - How do I sort a dictionary by value? - Stack Overflow
I sorted the list by keys first, then by values, but the order of the keys with the same value does not remain. 2012-06-18T10:04:04.617Z+00:00 ... Dicts can now be sorted, starting with CPython 3.6 and all other Python implementations starting with 3.7 2020-04-24T19:38:07.147Z+00:00 ... True at the time, but now python dictionaries ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › sort list of dictionaries by value in python?
Sort List of Dictionaries by Value in Python? - Spark By {Examples}
May 31, 2024 - We can sort a list of dictionaries by value using sorted() or sort() function in Python. Sorting is always a useful utility in everyday programming. Using
🌐
PythonHow
pythonhow.com › how › sort-a-list-of-dictionaries-by-a-value-of-the-dictionary
Here is how to sort a list of dictionaries by a value of the dictionary in Python
New: Practice Python, JavaScript ... 'age': 30}, {'name': 'Bob', 'age': 20}, ] # Sort the list of dictionaries by the age value sorted_list = sorted(my_list, key=lambda x: x['age']) # Print the sorted list print(sorted_list)...
🌐
Reddit
reddit.com › r/learnpython › how to sort a list containing dictionary items?
r/learnpython on Reddit: How to sort a list containing dictionary items?
September 30, 2023 -

Let's say i have a list from a json like this:

list = [
{
    "name": "Player1",
    "currency": "10"
},
{
    "name": "Player2",
    "currency": "15"
},
    {
    "name": "Player3",
    "currency": "7"
}

]

How do i sort it based on the amount of currency a player has so that i can get the richest players using simple list indexes? For example if i want the details of the second richest player, i sort the list based on the amount of currency player has and then do a simple list[1] to get their details. How do i achieve this?

Top answer
1 of 4
5
List objects have a sort function to sort the list in-place. This function has a key parameter to specify a function that will be called on each item in the list, after which the items will be sorted based on the results of that function. It also accepts a reverse parameter that, if set to True, will cause sort to sort the list in descending order. Let's say you have a list students that contains Student objects. To sort the students by age from oldest to youngest, you'd do students.sort(key = lambda x: x.Age, reverse=True). Sorting your list of dictionaries is done in pretty much the same way. See this if you need more examples. Keep in mind that in your list, the currency values are strings, not integers. You'll need to convert them first, otherwise they won't be sorted correctly. You can do that in the lambda function you pass to sort. Incidentally, using variable names like list is bad practice. You're overriding the built-in list class by doing that.
2 of 4
2
list_ = [ { "name": "Player1", "currency": "10" }, { "name": "Player2", "currency": "15" }, { "name": "Player3", "currency": "7" } ] new_list_ = sorted(list_, key=lambda x: int(x["currency"])) print(new_list_) NB. Strongly recommend against using Python types/names for variable names, as it makes it harder to access original features. Hence, appended _ to list from your example. new_list_[-2] will reference the second-richest player (well, not if they have matching currency levels. If you want that, you will need to collect the unique values, find the second highest, and then find the players with that amount. Note, sorted can use the reversed keyword. PS. Example to find second richest, amounts = sorted(set(int(x['currency']) for x in new_list_)) if len(amounts) > 1: second_highest = amounts[-2] print(f"{second_highest=}") for player in new_list_: if int(player['currency']) == second_highest: print(player["name"]) else: print("No player is second richest") NB. Might be easier to convert the currency values to numbers before doing anything else.
🌐
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.
Find elsewhere
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - Sort Python dictionaries by keys or values using sorted(), lambdas, and itemgetter. Take this quiz to check your understanding. Before Python 3.6, dictionaries were inherently unordered. A Python dictionary is an implementation of the hash table, which is traditionally an unordered data structure.
🌐
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.
🌐
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.
🌐
iO Flood
ioflood.com › blog › python-sort-dictionary-by-value
Python Sort Dictionary by Value | Handling Data Structures
August 13, 2024 - While sorted() and operator.itemgetter() are commonly used to sort dictionaries by value, Python provides other ways to achieve the same result. One such alternative is using lambda functions.
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.

🌐
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.
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-sort-list-dictionaries-values-python-using-lambda-function
Ways to sort list of dictionaries by values in Python - Using lambda function - GeeksforGeeks
November 14, 2025 - Sorted by age: [{'name': 'Kevin', 'age': 19}, {'name': 'Harry', 'age': 20}, {'name': 'Robin', 'age': 20}] ... sorted(dic, key=lambda x: x['age']): sorts the list of dictionaries in ascending order based on the 'age' value of each dictionary.
🌐
TutorialsPoint
tutorialspoint.com › article › How-do-I-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-Python
How do I sort a list of dictionaries by values of the dictionary in Python?
January 31, 2023 - The sorted() function returns a new sorted list from any iterable. The itemgetter from the operator module provides an efficient way to extract values from dictionaries for sorting.
🌐
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....
🌐
EnableGeek
enablegeek.com › home › how to sort list of dictionaries in python
How to Sort List of Dictionaries in Python - EnableGeek
April 2, 2024 - In Python, you can sort a list of dictionaries by a value using the ‘sorted‘ function and providing a ‘key‘ function that returns the value to sort by.
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-list-of-dictionaries-python-by-multiple-keys
Sort List of Dictionaries by Multiple Keys - Python - GeeksforGeeks
July 23, 2025 - For example, if we have the following ... 'Kunal', 'age': 25, 'score': 85}, {'name': 'Aryan', 'age': 25, 'score': 90}] sorted() function is a built-in Python function that sorts any iterable and returns a new sorted ...