Exploring Why Dictionaries Aren't Well Suited for Sorting
00:00 Dictionaries aren’t well suited for sorting. You’ve seen in the previous lesson that they don’t have methods to reorder the items in the same way that lists do, for example. Let’s see why this is a problem.
00:12
You can create a dictionary points
similar to the one from the previous lesson, but there are four players now, Jill, Jack, Jim, and Jane, and the values are the number of points that they have at a certain point during the game.
00:26
You can also create a list as you did in the previous lesson: points_as_list
. The list this time has four values: 10, 8, 12, 5.
00:36
You’ve seen in the previous lesson that since points_
as_list
is a list, you can call the sort()
method, which sorts the list in place.
00:46
This means that it doesn’t create a new list. Instead, it reorders the values within the same object, within the same list. points_
as_list
now has the values 5, 8, 10, 12. Same items as before, but in a different order.
01:04 Unfortunately, it’s not so straightforward to do the same thing with a dictionary. The methods to reorder items in a dictionary aren’t there. They’re not present. Let’s see how you could do it if you wanted to.
01:18 Let’s say you want to order this dictionary based on the number of points the players have from highest to lowest. So Jim should be first with 12 points.
01:25 Then Jill, Jack, and finally Jane. Let’s take the second placed person, Jill, and you could delete Jill entirely from the list, from the dictionary rather, we’re dealing with a dictionary.
01:42 And Jill is not there anymore. Jill is missing, but you could add Jill back
01:54 There’s Jill back in the dictionary with 10 points, but since you deleted Jill and added her back in, she’s now in the final place in the dictionary. A reminder that dictionaries remember the order of insertion.
02:09
Jill has been removed and then re-added, therefore she ends up in the last place. The item Jill: 10
is at the end.
02:20 Jack is next. We could delete Jack in the same way
02:27 and add Jack back in. And Jack had eight points.
02:33 And when we look at the dictionary, Jim and Jane haven’t moved except that we’ve removed items and placed them at the end because each time we add an item in a dictionary, they go at the end.
02:44
So Jill: 10
and Jack: 8
are the final two items.
02:50 Let’s take Jane. Jane is the last who’s left. She should be at the end so we could delete Jane and we can add her back in. But Jane has five points
03:10 and that means we have reordered this dictionary by deleting items and adding them again, and each time we add them again, they go at the end. This is the same object as we had before.
03:22 You did not create a new dictionary. It’s the same dictionary, but the items have been reordered. However, doing it this way is not ideal. In real applications, you’ll never reorder a dictionary in this manner.
03:38 Let’s recap. It’s difficult to sort dictionaries in place. Dictionaries are mutable objects, but they don’t have the methods you need to reorder the items.
03:50 You can delete a key-value pair and add it again to the dictionary. This process does place the item at the end of the order within the dictionary, but you generally do not want to sort a dictionary using this manual method.
04:06 In the following lessons, you look at different techniques to sort dictionaries in a better and more efficient manner.
Become a Member to join the conversation.