How do I sort a list of dictionaries by a key?

In Python, you can sort a list of dictionaries by a specific key using the built-in `sorted()` function along with a lambda function. Here's an example of how to do this:

# Sample list of dictionaries data = [ {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 22}, {'name': 'Charlie', 'age': 30} ] # Sorting the list of dictionaries by age sorted_data = sorted(data, key=lambda x: x['age']) print(sorted_data) # Output: [{'name': 'Bob', 'age': 22}, {'name': 'Alice', 'age': 25}, {'name': 'Charlie', 'age': 30}]

Python sort list dictionaries key sorted