I’m new to Python and I’m trying to sort a list of dictionaries based on a specific key within each dictionary. The dictionaries have the following structure:
python
Copy code
data = [
{'name': 'John', 'age': 25},
{'name': 'Jane', 'age': 30},
{'name': 'Alex', 'age': 20}
]
I want to sort the list of dictionaries based on the ‘age’ key in ascending order. How can I achieve this using Python?
Any help would be appreciated. Thank you!
2
Answers
You can use the following code:
Using the
sorted()
function with a custom sorting keyOutput