Skip to content

Effortlessly sort a dict by key in Python

[

Sorting a Python Dictionary: Values, Keys, and More

by Ian Currie

Sorting a dictionary in Python can be a daunting task. In this tutorial, we will explore various methods to sort dictionaries, including sorting by values, keys, and nested attributes. We will also discuss performance considerations and alternative data structures for key-value data.

Rediscovering Dictionary Order in Python

Before Python 3.6, dictionaries in Python were unordered. This means that the order of elements in a dictionary was not guaranteed. However, starting from Python 3.6, dictionaries started to retain the order of element insertion, thanks to the implementation of the compact dictionary. Since Python 3.7, the order of dictionaries is guaranteed.

Understanding What Sorting A Dictionary Really Means

When we talk about sorting a dictionary in Python, we usually mean sorting the key-value pairs of the dictionary based on certain criteria. The result of sorting a dictionary is not a new dictionary, but rather a sorted representation of the dictionary.

Sorting Dictionaries in Python

Python provides several methods for sorting dictionaries. Let’s explore some of these methods.

Using the sorted() Function

The sorted() function is a built-in function in Python that allows us to sort any iterable, including dictionaries. To sort a dictionary, we need to pass the dictionary items as the input to the sorted() function. By default, the sorted() function sorts the dictionary based on the keys.

ages = {'Alice': 23, 'Bob': 27, 'Charlie': 20}
sorted_ages = sorted(ages.items())
print(sorted_ages)

Output:

[('Alice', 23), ('Bob', 27), ('Charlie', 20)]

Getting Keys, Values, or Both From a Dictionary

In some cases, we may only need to sort the keys or values of a dictionary, or we may need to sort based on both the keys and values. Python provides methods to extract the keys, values, or items (key-value pairs) from a dictionary.

ages = {'Alice': 23, 'Bob': 27, 'Charlie': 20}
sorted_keys = sorted(ages.keys())
sorted_values = sorted(ages.values())
sorted_items = sorted(ages.items())
print(sorted_keys)
print(sorted_values)
print(sorted_items)

Output:

['Alice', 'Bob', 'Charlie']
[20, 23, 27]
[('Alice', 23), ('Bob', 27), ('Charlie', 20)]

Understanding How Python Sorts Tuples

When sorting a dictionary or any other iterable, Python uses the default comparison behavior for the elements in the iterable. For tuples, the comparison is done element-wise, starting from the first element. If the first elements are the same, the comparison moves to the next element, and so on.

people = [('Alice', 23), ('Bob', 27), ('Charlie', 20)]
# Sort by age (second element)
sorted_people = sorted(people, key=lambda x: x[1])
print(sorted_people)

Output:

[('Charlie', 20), ('Alice', 23), ('Bob', 27)]

Using the key Parameter and Lambda Functions

The sorted() function accepts a key parameter, which allows us to define a custom sorting criterion. We can use lambda functions to define these custom sorting criteria.

people = [{'name': 'Alice', 'age': 23}, {'name': 'Bob', 'age': 27}, {'name': 'Charlie', 'age': 20}]
# Sort by name length
sorted_people = sorted(people, key=lambda x: len(x['name']))
print(sorted_people)

Output:

[{'name': 'Bob', 'age': 27}, {'name': 'Alice', 'age': 23}, {'name': 'Charlie', 'age': 20}]

Selecting a Nested Value With a Sort Key

Sometimes, we may need to sort a dictionary based on a value that is nested within the dictionary. We can achieve this by specifying a sort key that accesses the nested value.

people = {'Alice': {'age': 23}, 'Bob': {'age': 27}, 'Charlie': {'age': 20}}
# Sort by age
sorted_people = sorted(people.items(), key=lambda x: x[1]['age'])
print(sorted_people)

Output:

[('Charlie', {'age': 20}), ('Alice', {'age': 23}), ('Bob', {'age': 27})]

Converting Back to a Dictionary

After sorting a dictionary, if we want to convert it back to a dictionary object, we can use the dict() constructor.

ages = {'Alice': 23, 'Bob': 27, 'Charlie': 20}
sorted_ages = sorted(ages.items())
sorted_dict = dict(sorted_ages)
print(sorted_dict)

Output:

{'Alice': 23, 'Bob': 27, 'Charlie': 20}

Considering Strategic and Performance Issues

When sorting dictionaries, we need to consider strategic and performance issues. Here are some things to keep in mind:

Using Special Getter Functions to Increase Performance and Readability

Instead of using lambda functions as the sort key, we can use special getter functions like itemgetter() from the operator module. These getter functions can increase performance and readability.

from operator import itemgetter
people = [{'name': 'Alice', 'age': 23}, {'name': 'Bob', 'age': 27}, {'name': 'Charlie', 'age': 20}]
# Sort by name length using itemgetter
sorted_people = sorted(people, key=itemgetter('name'))
print(sorted_people)

Output:

[{'name': 'Alice', 'age': 23}, {'name': 'Bob', 'age': 27}, {'name': 'Charlie', 'age': 20}]

Measuring Performance When Using itemgetter()

To compare the performance of different sorting methods, we can use the timeit module. This module allows us to time our code and get tangible results for performance comparison.

from operator import itemgetter
import timeit
people = [{'name': 'Alice', 'age': 23}, {'name': 'Bob', 'age': 27}, {'name': 'Charlie', 'age': 20}]
# Sort by name length using itemgetter
setup = 'from operator import itemgetter; ' \
'people = [{"name": "Alice", "age": 23}, {"name": "Bob", "age": 27}, {"name": "Charlie", "age": 20}]'
stmt = 'sorted(people, key=itemgetter("name"))'
# Measure performance
execution_time = timeit.timeit(stmt, setup=setup, number=1000000)
print(f"Execution time: {execution_time} seconds")

Output:

Execution time: 0.8824421 seconds

Judging Whether You Want to Use a Sorted Dictionary

Using a sorted dictionary is not a common pattern in Python. In most cases, a regular dictionary is sufficient. Consider whether a sorted dictionary is really necessary before implementing it in your code.

Comparing the Performance of Different Data Structures

Besides dictionaries, there are other data structures in Python that can store key-value data, such as lists of tuples, sets of tuples, or even custom classes. Depending on your use case, these alternative data structures might offer better performance or flexibility.

Comparing the Performance of Sorting

Sorting can be an expensive operation, especially for large datasets. Consider whether you need to sort your data frequently and whether there are alternative approaches that can provide similar functionality with better performance.

Comparing the Performance of Lookups

If you frequently need to access elements in a dictionary based on a specific key, consider using different data structures, like sets or indexing structures, that can provide faster lookup performance.

Conclusion

In this tutorial, we explored different methods to sort dictionaries in Python. We learned how to use the sorted() function, extract keys, values, or items from a dictionary, and specify custom sort keys using lambda functions or special getter functions. We also discussed performance considerations and alternative data structures for key-value data. By understanding these techniques, you can effectively sort dictionaries and optimize the performance of your Python code.