Sorting a List of Dictionaries by a Value in Python
Suppose you have a list of dictionaries, and you want to sort the list based on the value of a particular dictionary key. In Python, you can do this using the built-in sorted function.
Here is an example of a list of dictionaries:
data = [ {'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}, ]
To sort this list by the age value of each dictionary, you can use the following code:
sorted_data = sorted(data, key=lambda x: x['age']) print(sorted_data)
This will output the following sorted list:
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
You can also use the operator module's itemgetter function as the key argument for the sorted function. This can make the code a bit more readable, especially if you are sorting by multiple keys.
For example:
import operator sorted_data = sorted(data, key=operator.itemgetter('age')) print(sorted_data)
This will give you the same result as the previous example.
If you want to sort the list in descending order, you can pass the reverse=True argument to the sorted function.
For example:
sorted_data = sorted(data, key=lambda x: x['age'], reverse=True) print(sorted_data)
This will output the following sorted list:
[{'name': 'Charlie', 'age': 35}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 25}]
You can also sort by multiple keys by specifying a tuple as the key argument. The elements of the tuple should be the functions that extract the keys to sort by. The sorted function will sort by the first key in the tuple, and if two elements have the same value for that key, it will sort by the second key, and so on.
For example, to sort the list by age first, and then by name for elements with the same age, you can use the following code:
sorted_data = sorted(data, key=lambda x: (x['age'], x['name'])) print(sorted_data)
This will output the following sorted list:
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
To sort the list by age first, and then by name for elements with the same age, you can use the following code:
sorted_data = sorted(data, key=operator.itemgetter('age', 'name')) print(sorted_data)
This will output the same sorted list as the previous example.
You can also use the sorted function to sort a list of dictionaries in-place, rather than creating a new sorted list. To do this, you can pass the key argument and any other desired arguments to the list.sort method.
For example:
data.sort(key=lambda x: x['age']) print(data)
This will sort the data list in-place, so that the original list is now sorted by the age value of each dictionary.
It's also possible to sort a list of dictionaries by an attribute of the dictionaries, rather than a key in the dictionary itself. To do this, you can use the attrgetter function from the operator module as the key argument for the sorted function.
Here is an example of a list of dictionaries that have a Person class as the value for the 'person' key:
class Person: def __init__(self, name, age): self.name = name self.age = age data = [ {'person': Person('Alice', 25)}, {'person': Person('Bob', 30)}, {'person': Person('Charlie', 35)}, ]
To sort this list by the age attribute of the Person objects, you can use the following code:
sorted_data = sorted(data, key=lambda x: x['person'].age) print(sorted_data)
You can also use the attrgetter function to specify an attribute to sort by.
For example:
import operator sorted_data = sorted(data, key=operator.attrgetter('person.age')) print(sorted_data)