Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
16.4. Sorting a Dictionary¶
Previously, you have used a dictionary to accumulate counts, such as the frequencies of letters or words in a text. For example, the following code counts the frequencies of different numbers in the list.
The dictionary’s keys are not sorted in any particular order. In fact, you may get a different order of output than someone else running the same code. We can force the results to be displayed in some fixed ordering, by sorting the keys.
With a dictionary that’s maintaining counts or some other kind of score, we might prefer to get the outputs sorted based on the count rather than based on the items. The standard way to do that in python is to sort based on a property of the key, in particular its value in the dictionary.
Here things get a little confusing because we have two different meaning of the word “key”. One meaning is a key in a dictionary. The other meaning is the parameter name for the function that you pass into the sorted function.
Remember that the key function always takes as input one item from the sequence and returns a property of the item. In our case, the items to be sorted are the dictionary’s keys, so each item is one key from the dictionary. To remind ourselves of that, we’ve named the parameter in tha lambda expression k. The property of key k that is supposed to be returned is its associated value in the dictionary. Hence, we have the lambda expression
lambda k: d[k].
Here’s a version of that using a named function.
When we sort the keys, passing a function with
key=lambda x: d[x] does not specify to sort the keys of a
dictionary. The lists of keys are passed as the first parameter value in the invocation of sort. The key parameter
provides a function that says how to sort them.
An experienced programmer would probably not even separate out the sorting step. And they might take advantage of the fact that when you pass a dictionary to something that is expecting a list, its the same as passing the list of keys.
Eventually, you will be able to read code like that and immediately know what it’s doing. For now, when you come
across something confusing, like line 11, try breaking it down. The function
sorted is invoked. Its first parameter
value is a dictionary, which really means the keys of the dictionary. The second parameter, the key function, decorates
the dictionary key with a post-it note containing that key’s value in dictionary d. The last parameter, True, says to
sort in reverse order.
There is another way to sort dictionaries, by calling .items() to extract a sequence of (key, value) tuples, and then sorting that sequence of tuples. But it’s better to learn the pythonic way of doing it, sorting the dictionary keys using a key function that takes one key as input and looks up the value in the dictionary.
Check Your Understanding
2. Sort the following dictionary based on the keys so that they are sorted a to z. Assign the resulting value to the variable
3. Below, we have provided the dictionary
groceries, whose keys are grocery items, and values are the number of each item that you need to buy at the store. Sort the dictionary’s keys into alphabetical order, and save them as a list called
4. Sort the following dictionary’s keys based on the value from highest to lowest. Assign the resulting value to the variable