0

To use the sort method in Python to sort a list of dictionaries by a specific key in the dictionary, you can pass a lambda function as the key parameter to the sort method. This lambda function should specify that the list should be sorted by the desired key in the dictionary.

my_list = [    {'name': 'Alice', 'age': 25},    {'name': 'Bob', 'age': 27},    {'name': 'Charlie', 'age': 23},]
my_list.sort(key x: x['age'])
print(my_list)
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

0 Answers0