How to Sort a List of Dictionaries in Python
If you have a list where each item is a dictionary, you can sort it by one dictionary key such as age, name, price, or city.
This is common when working with real data from JSON files, APIs, or form input.
Quick answer
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22},
{"name": "Cara", "age": 27}
]
sorted_people = sorted(people, key=lambda person: person["age"])
print(sorted_people)
Output:
[{'name': 'Ben', 'age': 22}, {'name': 'Cara', 'age': 27}, {'name': 'Ana', 'age': 30}]
Use sorted() when you want a new sorted list. Use a key function to choose which dictionary value to sort by.
What this page helps you do
- Sort a list of dictionaries by a specific key
- Sort numbers like age or price
- Sort text like name or city
- Reverse the order when needed
- Avoid common beginner mistakes
Use sorted() with a key
A list of dictionaries looks like this:
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22},
{"name": "Cara", "age": 27}
]
Each item in the list is a dictionary.
To sort this kind of data, use sorted() and tell Python which value to compare.
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22},
{"name": "Cara", "age": 27}
]
sorted_people = sorted(people, key=lambda item: item["age"])
print(sorted_people)
Output:
[{'name': 'Ben', 'age': 22}, {'name': 'Cara', 'age': 27}, {'name': 'Ana', 'age': 30}]
What each part does
sorted(people, ...)sorts the listkey=tells Python what value to use for sortinglambda item: item["age"]gets theagefrom each dictionary
If lambda is new to you, see how to use lambda functions in Python.
Sort by a number value
This is useful for keys like age, score, price, or quantity.
products = [
{"name": "Notebook", "price": 4.50},
{"name": "Pen", "price": 1.20},
{"name": "Bag", "price": 18.00}
]
sorted_products = sorted(products, key=lambda item: item["price"])
print(sorted_products)
Output:
[{'name': 'Pen', 'price': 1.2}, {'name': 'Notebook', 'price': 4.5}, {'name': 'Bag', 'price': 18.0}]
Python sorts number values from lowest to highest by default.
Sort numbers in descending order
Use reverse=True if you want highest to lowest.
products = [
{"name": "Notebook", "price": 4.50},
{"name": "Pen", "price": 1.20},
{"name": "Bag", "price": 18.00}
]
sorted_products = sorted(products, key=lambda item: item["price"], reverse=True)
print(sorted_products)
Output:
[{'name': 'Bag', 'price': 18.0}, {'name': 'Notebook', 'price': 4.5}, {'name': 'Pen', 'price': 1.2}]
If you only need to sort a normal list of values, see how to sort a list in Python.
Sort by a string value
You can also sort alphabetically by a string key like name, title, or city.
people = [
{"name": "Cara", "age": 27},
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22}
]
sorted_people = sorted(people, key=lambda item: item["name"])
print(sorted_people)
Output:
[{'name': 'Ana', 'age': 30}, {'name': 'Ben', 'age': 22}, {'name': 'Cara', 'age': 27}]
Case-sensitive sorting
Uppercase and lowercase letters can change the result.
people = [
{"name": "ana"},
{"name": "Ben"},
{"name": "cara"}
]
sorted_people = sorted(people, key=lambda item: item["name"])
print(sorted_people)
This may not sort the way you expect, because Python compares strings by their character values.
Case-insensitive sorting
Use .lower() if you want a more natural alphabetical order.
people = [
{"name": "ana"},
{"name": "Ben"},
{"name": "cara"}
]
sorted_people = sorted(people, key=lambda item: item["name"].lower())
print(sorted_people)
Output:
[{'name': 'ana'}, {'name': 'Ben'}, {'name': 'cara'}]
Sort the original list in place
If you want to change the original list instead of creating a new one, use .sort().
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22},
{"name": "Cara", "age": 27}
]
people.sort(key=lambda item: item["age"])
print(people)
Output:
[{'name': 'Ben', 'age': 22}, {'name': 'Cara', 'age': 27}, {'name': 'Ana', 'age': 30}]
Important difference
sorted()returns a new list.sort()changes the original list.sort()returnsNone
This is a common beginner mistake:
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": 22}
]
result = people.sort(key=lambda item: item["age"])
print(result)
Output:
None
If you want a new list, use sorted() instead.
Handle missing keys safely
Using item["age"] works only if every dictionary has an age key.
If one dictionary is missing that key, Python raises a KeyError.
people = [
{"name": "Ana", "age": 30},
{"name": "Ben"},
{"name": "Cara", "age": 27}
]
sorted_people = sorted(people, key=lambda item: item["age"])
print(sorted_people)
That fails because {"name": "Ben"} does not have an age.
Safer approach with get()
Use .get() when keys may be missing.
people = [
{"name": "Ana", "age": 30},
{"name": "Ben"},
{"name": "Cara", "age": 27}
]
sorted_people = sorted(people, key=lambda item: item.get("age", 0))
print(sorted_people)
Output:
[{'name': 'Ben'}, {'name': 'Cara', 'age': 27}, {'name': 'Ana', 'age': 30}]
Here, missing age values are treated as 0.
Choose a default value that makes sense for your data.
- Use
0if missing numbers should come first - Use a large number if missing values should come last
- Use
""for missing text values in some cases
To learn more about this method, see Python dictionary get() method. If you are seeing a missing-key error, read KeyError in Python: causes and fixes.
When sorting may fail
Sorting can fail if the values are not consistent.
Mixed types
This usually causes a TypeError:
people = [
{"name": "Ana", "age": 30},
{"name": "Ben", "age": "22"},
{"name": "Cara", "age": 27}
]
sorted_people = sorted(people, key=lambda item: item["age"])
print(sorted_people)
One age is an integer, but another is a string. Python cannot always compare them.
Missing keys
This raises a KeyError when you use item["age"] and one dictionary does not have that key.
None values
If some values are None, sorting may also fail or give unexpected results.
Before sorting, check what your data looks like:
print(data)
print(type(data))
print(data[0])
print(data[0].keys())
print([item.get('age') for item in data])
print([type(item.get('age')) for item in data])
These checks help you answer questions like:
- Is
datareally a list? - Is each item a dictionary?
- Do all dictionaries have the same keys?
- Are all
agevalues the same type?
Common mistakes
- Using
item["key"]when some dictionaries do not have that key - Trying to sort mixed types like integers and strings together
- Using
.sort()and expecting it to return a new list - Forgetting
reverse=Truewhen descending order is needed - Getting unexpected string order because of uppercase and lowercase letters
FAQ
What is the easiest way to sort a list of dictionaries in Python?
Use sorted() with key=.
sorted(data, key=lambda item: item["age"])
What is the difference between sorted() and sort()?
sorted()creates a new listlist.sort()changes the original list
How do I sort in descending order?
Add reverse=True.
sorted(data, key=lambda item: item["age"], reverse=True)
How do I sort alphabetically by name?
Use a string key:
sorted(data, key=lambda item: item["name"])
How do I avoid errors when a key is missing?
Use .get() with a default value:
sorted(data, key=lambda item: item.get("age", 0))
See also
- Python
sorted()function explained - Python dictionary
get()method - How to sort a list in Python
- How to filter a list in Python
- How to use lambda functions in Python
- KeyError in Python: causes and fixes
Once you are comfortable sorting simple examples, the same idea works for real-world data such as dictionaries loaded from files, JSON, or API responses.