Effortlessly Sort Python Dictionary by Value
Sorting a Python Dictionary: Values, Keys, and More
by Ian Currie
You’ve got a dictionary, but you’d like to sort the key-value pairs. Perhaps you’ve tried passing a dictionary to the sorted()
function but haven’t gotten the results you expected. In this tutorial, you’ll go over everything you need to know if you want to sort dictionaries in Python.
In this tutorial, you’ll:
- Review how to use the
sorted()
function - Learn how to get dictionary views to iterate over
- Understand how dictionaries are cast to lists during sorting
- Learn how to specify a sort key to sort a dictionary by value, key, or nested attribute
- Review dictionary comprehensions and the
dict()
constructor to rebuild your dictionaries - Consider alternative data structures for your key-value data
Along the way, you’ll also use the timeit
module to time your code and get tangible results for comparing the different methods of sorting key-value data. You’ll also consider whether a sorted dictionary is really your best option, as it’s not a particularly common pattern.
To get the most out of this tutorial, you should know about dictionaries, lists, tuples, and functions. With that knowledge, you’ll be able to sort dictionaries by the end of this tutorial. Some exposure to lambda functions, such as lambda functions, will also come in handy but isn’t a requirement.
First up, you’ll learn some foundational knowledge before trying to sort a dictionary in Python.
Rediscovering Dictionary Order in Python
Before Python 3.6, dictionaries were inherently unordered. A Python dictionary is an implementation of the hash table, which is traditionally an unordered data structure.
As a side effect of the compact dictionary implementation in Python 3.6, dictionaries started to conserve insertion order. From 3.7, that insertion order has been guaranteed.
If you wanted to keep an ordered dictionary as a data structure before Python 3.6, you would have had to use OrderedDict from the collections module.
Understanding What Sorting A Dictionary Really Means
Sorting a dictionary might sound like a simple task at first. You might think that the sorted()
function should be able to handle it. However, dictionaries in Python are inherently unordered, which means the order in which the key-value pairs are stored is not preserved.
When you sort a dictionary, you’re actually sorting the keys and then using those sorted keys to access the corresponding values. The result is a list of key-value pairs sorted by the keys.
Sorting Dictionaries in Python
Now that you understand the basics, let’s dive into the different methods you can use to sort dictionaries in Python.
Using the sorted() Function
The sorted()
function allows you to sort any iterable by specifying a key function to determine the sort order. To sort a dictionary, you can pass its keys to the sorted()
function, which will return a sorted list of keys.
Output:
Getting Keys, Values, or Both From a Dictionary
If you want to sort the dictionary by values, you can use the key
parameter of the sorted()
function and provide a lambda function that returns the values of each key.
Output:
If you want to sort the dictionary by both keys and values, you can use the items()
method of the dictionary to get a list of key-value pairs and then sort that list using the key parameter.
Output:
Understanding How Python Sorts Tuples
When you sort a list of tuples using the sorted()
function, Python uses a built-in comparison algorithm to determine the sort order. The algorithm compares the elements of each tuple in the list and sorts them based on the result of the comparison.
By default, Python compares the elements of each tuple in lexicographic order, meaning it compares the first element of each tuple, then the second element if the first elements are equal, and continues until it finds a difference or reaches the end of the tuple.
Using the key Parameter and Lambda Functions
If you want to specify a custom sort order for the values of each key, you can use the key
parameter of the sorted()
function and provide a lambda function.
The lambda function takes an element from the iterable and returns a value that will be used for the sort comparison. In the case of sorting dictionaries, you can use the lambda function to extract the values of each key and provide them for the sort comparison.
Output:
Selecting a Nested Value With a Sort Key
If your dictionary contains nested values, you can use the key
parameter and a lambda function to select a specific nested value as the sort key. This allows you to sort the dictionary based on that nested value.
Output:
Converting Back to a Dictionary
After sorting the dictionary, you might want to convert it back to a dictionary data structure. To do this, you can use a dictionary comprehension or the dict()
constructor.
Output:
Considering Strategic and Performance Issues
When working with sorted dictionaries, there are some strategic and performance considerations to keep in mind.
Using Special Getter Functions to Increase Performance and Readability
If you need to sort large dictionaries or dictionaries with complex nested values, using the itemgetter()
function from the operator
module can provide a performance boost. It provides a faster alternative to the lambda function when sorting dictionaries.
Output:
Measuring Performance When Using itemgetter()
To measure the performance of different methods of sorting dictionaries, you can use the timeit
module. This module allows you to time your code and get tangible results for comparing the different methods.
Output:
Judging Whether You Want to Use a Sorted Dictionary
While sorting dictionaries can be useful in some cases, it’s important to consider whether a sorted dictionary is really your best option. Sorting a dictionary requires additional time and memory compared to an unordered dictionary.
If you frequently need to access the dictionary in a specific order, it might be worth considering alternative data structures like lists or arrays that are specifically designed for ordered data.
Comparing the Performance of Different Data Structures
To compare the performance of different data structures for storing key-value data, you can use the timeit
module again and time the different methods.
Output:
Comparing the Performance of Sorting
You can also compare the performance of sorting different data structures by measuring the time taken for each method.
Output:
Comparing the Performance of Lookups
Lastly, you can compare the performance of lookups in different data structures by timing the access to a specific key.
Output:
Conclusion
In this tutorial, you learned how to sort dictionaries in Python using various methods. You explored the use of the sorted()
function, lambda functions, and the itemgetter()
function to sort dictionaries based on keys and values. You also considered performance issues and alternative data structures for storing key-value data.
Remember that sorting dictionaries is not a common pattern and should only be done when necessary. Consider the trade-offs in terms of time and memory before deciding to sort a dictionary. With the knowledge gained from this tutorial, you can effectively sort dictionaries in Python and optimize your code when dealing with key-value data.