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 - In Python, sorting a list of ... (TypeError) by default. By specifying the key argument of sort() or sorted(), you can sort a list of dictionaries according to the value of the specific key....
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
Sort a list of dictionaries by keys in Python - Stack Overflow
I know this can be done by getting key as list then sort them and then create another list, but i am looking for more elegant may be a one liner solution ... You aren't just sorting the dicts, but you are altering their content with some unspecified logics. ... Why are you making dictionaries that ... More on stackoverflow.com
🌐 stackoverflow.com
python - Sort list of dictionaries by key - Stack Overflow
I have a list of dictionaries where the order of the keys is not consistent. Ideally I would like 'vid' to always come first with 'name' being second. As you can see below, sometimes 'name' comes f... More on stackoverflow.com
🌐 stackoverflow.com
Sorting dict keys and a dict inside a dict
Dictionaries are not sortable. They preserve insertion order only. If you need sorted data, use a different data structure. More on reddit.com
🌐 r/learnpython
30
47
July 4, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-list-of-dictionaries-python-by-multiple-keys
Sort List of Dictionaries by Multiple Keys - Python - GeeksforGeeks
July 23, 2025 - In this method we create a list of tuples where each tuple contains the values of the keys we want to sort by followed by the original dictionary. This list of tuples is then sorted based on the values of the keys and the sorted dictionaries ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-python-dictionaries-by-key-or-value
Sort Python Dictionary by Key or Value - Python - GeeksforGeeks
Loop prints key-value pairs in ascending order of their values. 4. Using NumPy: This approach uses NumPy’s argsort() for fast value-based sorting in numerical dictionaries. Python · import numpy as np d = {'alex': 10, 'ben': 9, 'clara': 15, 'diana': 2, 'eva': 32} k = list(d.keys()) v = list(d.values()) idx = np.argsort(v) res = {k[i]: v[i] for i in idx} print(res) Output ·
Published   January 13, 2026
🌐
Medium
medium.com › @vipinc.007 › python-sort-a-list-of-dictionaries-by-key-d9347f592b2f
Python | sort a list of dictionaries by key | by Vipin Cheriyanveetil | Medium
February 21, 2023 - To sort this list by year, we can use the sorted() function, which takes a key function as an argument.
🌐
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 - The first script uses the sorted() function in combination with a lambda function to sort a list of dictionaries. The sorted() function is a built-in Python function that returns a new sorted list from the items in an iterable.
🌐
freeCodeCamp
freecodecamp.org › news › python-sort-dictionary-by-key
Python Sort Dictionary by Key – How to Sort a Dict with Keys
May 25, 2023 - The sorted list is iterated over, and each key-value pair is added to the sorted_dict using assignment. Another approach to sorting a dictionary by key is to use the collections.OrderedDict class from the Python standard library. This class is a dict subclass that remembers the order of its elements based on the insertion order.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-dictionaries-list-by-keys-value-list-index
Python - Sort dictionaries list by Key's Value list index - GeeksforGeeks
April 5, 2023 - This is done using a lambda function that accesses the K key and the idx index of the corresponding list for each dictionary. Store the sorted result in a variable called res. Print the result using the print() function and passing the string ...
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - In the following code, you’ll be using timeit to compare the time it takes to sort the two data structures by the age attribute: Language: Python Filename: compare_sorting_dict_vs_list.py · from timeit import timeit from samples import dictionary_of_dictionaries, list_of_dictionaries sorting_list = "sorted(list_of_dictionaries, key=lambda item:item['age'])" sorting_dict = """ dict( sorted( dictionary_of_dictionaries.items(), key=lambda item: item[1]['age'] ) ) """ sorting_list_time = timeit(stmt=sorting_list, globals=globals()) sorting_dict_time = timeit(stmt=sorting_dict, globals=globals()) print( f"""\ {sorting_list_time=:.2f} seconds {sorting_dict_time=:.2f} seconds list is {(sorting_dict_time/sorting_list_time):.2f} times faster""" )
🌐
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.
🌐
CodeConverter
codeconverter.com › articles › python-sort-list-of-dictionaries
Python: Sort a List of Dictionaries by Key Value | CodeConverter Blog
February 12, 2026 - The simplest way to sort a list of dictionaries is by using the built-in sorted() function with a key function. The key function takes a dictionary and returns the value you want to sort on. In the example above, we're sorting on the 'price' key. But what if you want to sort on multiple keys?
🌐
LabEx
labex.io › tutorials › python-how-to-sort-a-list-of-dictionaries-by-a-key-in-python-398246
How to sort a list of dictionaries by a key in Python | LabEx
In this example, we use the key parameter of the sorted() function to specify that we want to sort the list based on the 'age' key of each dictionary. Another way to sort a list of dictionaries is by using the itemgetter() function from the ...
🌐
Better Stack
betterstack.com › community › questions › how-to-sort-list-of-dictionaries-in-python
How do I sort a list of dictionaries by a value of the dictionary in Python? | Better Stack Community
January 26, 2023 - In Python, you can use the sorted() function to sort a list of dictionaries by a specific value of the dictionary. The sorted() function takes two arguments: the list to be sorted, and a key function that maps each element of the list to a value ...
🌐
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 following example demonstrates how OrderedDict maintains the original insertion order of items with duplicate values: ... The student_scores dictionary is sorted by values in ascending order. Since both 'Charlie' and 'Brayne' have the same value (85), OrderedDict preserves their original insertion order. This ensures a stable and predictable result, even when values are duplicated. In this article, we explored how to sort Python dictionaries by keys—to organize entries alphabetically or numerically—and by values—to rank data based on importance or frequency.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python sort dictionary by key
Python Sort Dictionary by Key - Spark By {Examples}
May 31, 2024 - We can sort the dictionary by key using a sorted() function in Python. It can be used to sort dictionaries by key in ascending order or
🌐
Temp Mail
tempmail.us.com › temp mail › blog › python › using a specific key to sort a list of dictionaries in python
Using a Specific Key to Sort a List of Dictionaries in Python
July 24, 2024 - The built-in Python method sorted() generates a new sorted list from an iterable. Using a lambda function as the key parameter allows us to define the dictionary key ('name') to sort by.
🌐
30 Seconds of Code
30secondsofcode.org › home › dictionary › sort dictionary list using a tuple key
Sort Python dictionary list using a tuple key - 30 seconds of code
January 4, 2023 - Luckily, the sorted() function can be used to sort a list of dictionaries using a tuple key. Simply return a tuple with the order of keys you want to sort by and the sorted() function will do the rest.
🌐
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
# Import the operator module import operator # Define a list of dictionaries my_list = [ {'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 30}, {'name': 'Bob', 'age': 20}, ] # Sort the list of dictionaries by the age value sorted_list = sorted(my_list, key=operator.itemgetter('age')) # Print the sorted list print(sorted_list)This will produce the same output as the previous example. ... Solve Python exercises and get instant AI feedback on your solutions.