Sorting a List of Dictionaries in Python: A Comprehensive Guide
In Python, you can sort a list of dictionaries based on a specific key using the built-in `sorted()` function or the list's `sort()` method. This can be incredibly useful when you need to organize data based on a particular attribute. Let's dive into how you can achieve this with clear examples.
Using the `sorted()` function
The `sorted()` function returns a new sorted list from the elements of any sequence. It doesn't modify the original list. Here's how you can use it to sort a list of dictionaries:
```python # Sample list of dictionaries list_of_dicts = [ {'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 30}, {'name': 'Jim', 'age': 22}, ] # Sort by 'age' key sorted_list = sorted(list_of_dicts, key=lambda x: x['age']) print(sorted_list) ```
In this example, the list is sorted based on the 'age' key in ascending order. If you want to sort in descending order, you can use the `reverse` parameter:

```python sorted_list = sorted(list_of_dicts, key=lambda x: x['age'], reverse=True) ```
Using the `sort()` method
The `sort()` method sorts the list in-place, meaning it modifies the original list. Here's how you can use it:
```python # Sort the original list in-place list_of_dicts.sort(key=lambda x: x['age']) ```
Again, to sort in descending order, use the `reverse` parameter:
```python list_of_dicts.sort(key=lambda x: x['age'], reverse=True) ```
Sorting by multiple keys
You can also sort by multiple keys. To do this, provide a tuple of keys to the `key` parameter. The list will be sorted first by the first key, then by the second key if the first keys are equal, and so on:

```python list_of_dicts.sort(key=lambda x: (x['age'], x['name'])) ```
Sorting with Custom Comparators
Sometimes, you might want to sort based on a custom comparator function. You can achieve this by providing a function to the `key` parameter that returns a value to sort by. Here's an example where we sort by the length of the 'name' key:
```python list_of_dicts.sort(key=lambda x: len(x['name'])) ```
Sorting with `itemgetter`
Python's `operator` module provides a function called `itemgetter` that can simplify sorting by a specific key. Here's how you can use it:
```python from operator import itemgetter list_of_dicts.sort(key=itemgetter('age')) ```
Using `itemgetter` can make your code more readable, especially when sorting by multiple keys:

```python list_of_dicts.sort(key=itemgetter('age', 'name')) ```
Sorting with `attrgetter`
Similarly, the `attrgetter` function from the `operator` module can be used to sort by attributes of objects. However, since we're working with dictionaries, `itemgetter` is more appropriate.
Sorting with `sorted()` and `itemgetter`
You can also use `itemgetter` with the `sorted()` function for a more explicit separation of sorting and original data:
```python from operator import itemgetter sorted_list = sorted(list_of_dicts, key=itemgetter('age')) ```
Conclusion
In this article, we've explored various ways to sort a list of dictionaries in Python. Whether you're using the `sorted()` function or the `sort()` method, and whether you're sorting by a single key or multiple keys, Python provides several options to suit your needs. Happy sorting!






















