Loading video player…

Understanding the Order of Items in a Dictionary

00:00 Sorting data structures depends on the order of the items. However, prior to Python 3.6, dictionaries had no order. Changes were made in Python 3.6 to how dictionaries are implemented and as a side effect of these changes, the order of insertion of items is now maintained.

00:18 This was made a formal part of the language in Python 3.7. This means that from Python 3.7 onwards, you have a guarantee that the order in which you add items in a dictionary is maintained and you can rely on it.

00:32 However, this does not make dictionaries sequences like lists or tuples, even though the order is maintained, they are not ordered structures in the same way as lists are.

00:43 Let’s explore this in the Python REPL.

00:46 Let’s start with a dictionary similar to the one you’ve seen earlier. This one only has two names in it, Jill with 10 points and Jack with eight points. Let’s assume these are points in a game that Jill and Jack are playing.

00:59 Let’s also create a list

01:02 with just the points, so not the names. [10, 8]. Now a list is a sequence, therefore, we can rely on the position of the items in the list. For example, we can always fetch the first item, the one with index zero, and that will always be 10. Dictionaries, the order is maintained, but I can’t use the order to fetch an item from a dictionary.

01:28 In this case, we get a key error because there’s no key zero in our dictionary. Instead, we would need to use the key, for example, Jill, and that will tell us Jill has 10 points.

01:42 Where’s the difference? Let us compare. So points is our dictionary, Jill 10, Jack 8. Let’s compare this dictionary with another dictionary, which is going to be fairly similar, but I’m going to put Jack first, also with 8 points.

01:58 So I haven’t changed the number of points that Jack has, but I’ve simply placed Jack first and Jill is second, also with 10 points. So are these two dictionaries identical?

02:11 The original one had Jill in first place and Jack in second place, whereas now I have Jack and Jill and the answer is True. These dictionaries are considered equal because each key has the same value.

02:26 It does not matter that the order is different. This is not true for lists. For example, if I had to check whether 10, 8 is equal to 8, 10, even though the values are the same, the order is not, therefore they are not equal.

02:44 Let’s display points_as_list again, which is the list: [10, 8]. Now lists also have another useful built-in method, which is .sort(), and this allows us to sort lists in place, directly in place.

03:01 Let’s have a look at points_as_list now. It has now been sorted with 8 being first and 10 being the second element. Lists, because they’re ordered, data structures have built-in features through their methods to reorder their elements, insert items at a specific position in the list. Dictionaries, however, do not have such methods.

03:26 When you need to sort a dictionary, you cannot rely on any of its built-in methods.

03:33 Let’s review the key points from this lesson before moving on to the next one. Dictionaries weren’t ordered before Python 3.6. This means you couldn’t sort a dictionary and keep it as a dictionary.

03:44 However, the changes made ensure that the order of insertion is now maintained and this order is now guaranteed since Python 3.7 when this became a formal part of the language.

03:55 Still, dictionaries aren’t sequences and they don’t have the same methods such as lists do that can be used to reorder elements.

04:05 In the next lesson, you’ll see yet another reason why dictionaries aren’t well suited to be sorted.

Become a Member to join the conversation.