You’ve got a dictionary, but you’d like to sort the key-value pairs. Perhaps you’ve tried passing a dictionary to the Show
In this tutorial, you’ll:
Along the way, you’ll also use the 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 higher-order 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 PythonBefore 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 compact dictionaries, then you could use Another alternative for storing an ordered key-value pair data is to store the pairs as a list of tuples. As you’ll see later in the tutorial, using a list of tuples could be the best choice for your data. An essential point to understand when sorting dictionaries is that even though they conserve insertion order, they’re not considered a sequence. A dictionary is like a set of key-value pairs, and sets are unordered. Dictionaries also don’t have much reordering functionality. They’re not like lists, where you can insert elements at any position. In the next section, you’ll explore the consequences of this limitation further. Understanding What Sorting A Dictionary Really MeansBecause dictionaries don’t have much reordering functionality, when sorting a dictionary, it’s rarely done in-place. In fact, there are no methods for explicitly moving items in a dictionary. If you wanted to sort a dictionary in-place, then you’d have to use the The The typical method for sorting dictionaries is to get a dictionary view, sort it, and then cast the resulting list back into a dictionary. So you effectively go from a dictionary to a list and back into a dictionary. Depending on your use case, you may not need to convert the list back into a dictionary. With those preliminaries out of the way, you’ll get to sorting dictionaries in the next section. Sorting Dictionaries in PythonIn this section, you’ll be putting together the components of sorting a dictionary so that, in the end, you can master the most common way of sorting a dictionary: >>>
Don’t worry if you don’t understand the snippets above—you’ll review it all step-by-step in the following sections. Along the way, you’ll learn how to use the Using the sorted() FunctionThe critical function that you’ll use to sort dictionaries is the built-in To illustrate the >>>
As you can see, the >>>
Sorting by numerical or alphabetical precedence is the most common way to sort elements, but maybe you need more control. Say you want to sort on the second character of each word in the last example. To
customize what the A callback function is a function that’s passed as an argument to another function. For In the following example, the function passed as the key accepts a string and will return the second character of that string: >>>
The More examples and explanations of the If you take another look at the results of this last sorting, you may notice the stability of the So, how about dictionaries? You can actually take the dictionary and feed it straight into the >>>
But the default behavior of passing in a dictionary directly to the Getting Keys, Values, or Both From a DictionaryIf you want to conserve all the information from a dictionary when sorting it, the typical first step is to call the >>>
The >>>
You’ll notice that any updates to the dictionary also get reflected in the view because they’re linked. A view represents a lightweight way to iterate over a dictionary without generating a list first. Crucially, you can use the
>>>
This example results in a sorted list of tuples, with each tuple representing a key-value pair of the dictionary. If you want to end up with a dictionary sorted by values, then you’ve still got two issues. The default behavior still seems to sort by key and not value. The other issue is that you end up with a list of tuples, not a dictionary. First, you’ll figure out how to sort by value. Understanding How Python Sorts TuplesWhen using the When comparing tuples, Python behaves a lot like it’s sorting strings alphabetically. That is, it sorts them lexicographically. Lexicographical sorting means that if you have two tuples, So, to order the tuples Because of Python’s lexicographic sorting behavior
for tuples, using the Using the key Parameter and Lambda FunctionsFor example, if you want to sort by value, then you have to specify a sort key. A
sort key is a way to extract a comparable value. For instance, if you have a pile of books, then you might use the author surname as the sort key. With the To see a sort key in action, take a look at this example, which is similar to the one you saw in the section introducing the >>>
In this example, you try out two ways of passing a The Since the default behavior of In the next section, you’ll take sort keys a bit further and use them to sort by a nested value. Selecting a Nested Value With a Sort KeyYou can also go further and use a sort key to select nested values that may or may not be present and return a default value if they’re not present:
In this
example, you have a dictionary with numeric keys and a nested dictionary as a value. You want to sort by the combined Python and JavaScript skills, attributes found in the Part of what makes sorting by the combined skill tricky is that the You’ve also used the You’ve successfully used a higher-order function as a sort key to sort a dictionary view by value. That was the hard part. Now there’s only one issue left to solve—converting the list that Converting Back to a DictionaryThe only issue left to address with the default behavior of You can iterate over the result with a >>>
This method gives you absolute control and flexibility in deciding how you want to construct your dictionary. This method can be quite lengthy to type out, though. If you don’t have any special requirements for constructing your dictionary, then you may want to go for a dictionary constructor instead: >>>
That’s nice and compact! You could also use a dictionary comprehension, but that only makes sense if you want to change the shape of the dictionary or swap the keys and values, for example. In the following comprehension, you swap the keys and values: >>>
Depending on how familiar you or your team are with comprehensions, this may be less readable than just using a normal Congratulations, you’ve got your sorted dictionary! You can now sort it by any criteria that you’d like. Now that you can sort your dictionary, you might be interested in knowing if there are any performance implications to using a sorted dictionary, or whether there are alternative data structures for key-value data. Considering Strategic and Performance IssuesIn this section, you’ll be taking a quick peek at some performance tweaks, strategic considerations, and questions to ask yourself about how you’ll use your key-value data. You’ll be leveraging the
Finally, note that you won’t be going into detail about how to use Using Special Getter Functions to Increase Performance and ReadabilityYou may have noticed that most of the sort key functions that you’ve used so far aren’t doing very much. All the function does is get a value from a tuple. Making a getter function is such a common pattern that Python has a special way to create special functions that get values more quickly than regular functions. The You
pass That’s right, it’s a function that returns a function. Using the The getter object from >>>
In the example, you start off with a tuple, similar to one that you might get as part of a dictionary view. You make the first getter by passing You can use this itemgetter as a key for the >>>
In this example, you start by using Finally, the example shows what would happen if you used You can use the function produced by >>>
The Measuring Performance When Using itemgetter()So, you end up with a function that behaves like the original
This code uses the Running this script from the shell should give you similar results to what’s below:
A savings of around 40 percent is significant! Bear in mind that when timing code execution, times can vary significantly between systems. That said, in this case, the ratio should be relatively stable across systems. From the results of this test, you can see that using Now you can squeeze a bit more performance out of your dictionary sorting, but it’s worth taking a step back and considering whether using a sorted dictionary as your preferred data structure is the best choice. A sorted dictionary isn’t a very common pattern, after all. Coming up, you’ll be asking yourself some questions about what you what you want to do with your sorted dictionary and whether it’s the best data structure for your use case. Judging Whether You Want to Use a Sorted DictionaryIf you’re considering making a sorted key-value data structure, then there are a few things you might want to take into consideration. If you’re going to be adding data to a dictionary, and you want it to stay sorted, then you might be better off using a structure like a list of tuples or a list of dictionaries:
A list of dictionaries is the most widespread pattern because of its cross-language compatibility, known as language interoperability. Language interoperability is especially relevant if you create an HTTP REST API, for instance. Making your data available over the Internet will likely mean serializing it in JSON. If someone using JavaScript were to consume JSON data from a REST API, then the equivalent data structure would be an object. The kicker is that JavaScript objects are not ordered, so the order would end up scrambled! This scrambling behavior would be true for many languages, and objects are even defined in the JSON specification as an unordered data structure. So, if you took care to order your dictionary before serializing to JSON, it wouldn’t matter by the time it got into most other environments. Another option is to simply not worry about ordering the data if you don’t need to. Including
With a What are the performance trade-offs with using a list of dictionaries versus a dictionary of dictionaries, though? In the next section, you’ll start to get some data on just that very question. Comparing the Performance of Different Data StructuresIf performance is a consideration—maybe you’ll be working with large datasets, for example—then you should carefully consider what you’ll be doing with the dictionary. The two main questions you’ll seek to answer in the next few sections are:
Once you’ve decided what usage patterns you’ll be subjecting your data structure to, then you can use the In this example, you’ll be pitting a dictionary of dictionaries against a list of dictionaries to see how they differ in terms of performance. You’ll be timing sorting operations and lookup operations with the following sample data:
Each data structure has the same information, except one is structured as a dictionary of dictionaries, and the other is a list of dictionaries. First up, you’ll be getting some metrics on the performance of sorting these two data structures. Comparing the Performance of SortingIn the following code, you’ll be using
This code imports the sample data structures for sorting on the Running the code for this test on the command line should provide you with some interesting results:
Sorting a list can be almost twice as fast as the process required to sort a dictionary view and then create a new sorted dictionary. So, if you plan on sorting your data very regularly, then a list of tuples might be better than a dictionary for you. One of the main overheads when sorting a dictionary, as opposed to a list, is reconstructing the dictionary after sorting it. If you were to take out the outer In the next section, you’ll be looking at the time it takes to look up values in a dictionary of dictionaries versus in a list of dictionaries. Comparing the Performance of LookupsHowever, if you plan to use the dictionary to sort your data once and use that dictionary mainly for lookups, then a dictionary will definitely make more sense than a list:
This code makes a series of lookups to both the list and the dictionary. You’ll note that with the list, you have to write a special function to make a lookup. The function to make the list lookup involves going through all the list elements one by one until you find the target element, which isn’t ideal. Running this comparison script from the command line should yield a result showing that dictionary lookups are significantly faster:
Nearly eighteen times faster! That’s a whole bunch. So, you certainly want to weigh the blazing speed of dictionary lookups against the data structure’s slower sorting. Bear in mind that this ratio can vary significantly from system to system, not to mention the variation that might come from differently sized dictionaries or lists. Dictionary lookups are certainly faster, though, no matter how you slice it. That said, if you’re just doing lookups, then you could just as easily do that with a regular unsorted dictionary. Why would you need a sorted dictionary in that case? Leave your use case in the comments! Now you should have a relatively good idea of some trade-offs between two ways to store your key-value data. The conclusion that you can reach is that, most of the time, if you want a sorted data structure, then you should probably steer clear of the dictionary, mainly for language interoperability reasons. That said, give Grant Jenks’ aforementioned sorted dictionary a try. It uses some ingenious strategies to get around typical performance drawbacks. Do you have any interesting or performant implementations of a sorted key-value data structure? Share them in the comments, along with your use cases for a sorted dictionary! ConclusionYou’ve gone from the most basic way to sort a dictionary to a few advanced alternatives that consider performance in sorting key-value pairs. In this tutorial, you’ve:
You’re now ready to not only sort dictionaries by any criteria you might think of, but also to judge whether the sorted dictionary is the best choice for you. Share your sorted dictionary use cases and performance comparisons in the comments below! Which method would you use to get all the elements in a dictionary returned as a list of tuple?The items() method will return each item in a dictionary, as tuples in a list.
What would you use to get the number of elements in a dictionary?To find the number of elements stored in a dictionary we can use the len() function.
Which method can be used to place an item at the end of a list?append() adds the new elements as another list, by appending the object to the end. To actually concatenate (add) lists together, and combine all items from one list to another, you need to use the . extend() method.
Which of the following methods can be called on a tuple?There are only two tuple methods count() and index() that a tuple object can call.
|