You're browsing snippets anonymously. Log in or create an account to save and manage your own snippets.
Public Snippets
Showing
1 snippet
with
tags:
sorting
Showing
1 snippet
Snippet
data = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'London'},
{'name': 'Charlie', 'age': 20, 'city': 'New York'}
]
# Sort by 'city' first, then by 'age' in descending order
sorted_data = sorted(data, key=lambda x: (x['city'], -x['age']))
print(sorted_data)
Description
More examples can be found at https://www.geeksforgeeks.org/sort-list-of-dictionaries-python-by-multiple-keys/
By
xtream1101
•
•
Updated
2025-02-06 13:27