Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Exploring the Unique Features of OrderedDict

In this lesson, you’ll learn about the extra and enhanced methods that OrderedDict offers: .move_to_end() and .popitem(). In the process, you’ll use lambda functions.

00:00 Exploring Unique Features of OrderedDict. Since Python 3.6, regular dictionaries have kept their items in the same order that they were inserted into the underlying dictionary.

00:12 This limits the usefulness of OrderedDict, as you’ve seen so far. However, OrderedDict provides some unique features that you can’t find in a dictionary.

00:22 With an ordered dictionary, you have access to these extra and enhanced methods. .move_to_end() allows you to move an existing item either to the end or the beginning of a dictionary.

00:33 .popitem() is an enhanced variation of its dictionary counterpart that allows you to remove and return an item from either the end or the beginning of the underlying ordered dictionary. OrderedDict and dict also behave differently when they’re tested for equality.

00:49 Specifically, when you compare ordered dictionaries, the order of items matters. That’s not the case with regular dictionaries. Finally, OrderedDict provides an attribute called .__dict__ that you can’t find in a regular dictionary instance.

01:03 This attribute allows you to add custom writable attributes to an existing ordered dictionary. One of the most useful, unique features of OrderedDict is that it has an extra method called .move_to_end().

01:16 This method allows you to move existing items to either the end or the beginning of the underlying dictionary, so it’s a great tool for reordering. When you use .move_to_end(), you can supply two arguments.

01:28 key holds the key that identifies the item that you want to move. If key doesn’t exist, then you get a KeyError. last holds a Boolean value that defines to which end of the dictionary you want to move the item. It defaults to True, which means the item will be moved to the end, or right side, of the dictionary.

01:45 False means the item will be moved to the front, or left side, of the dictionary. On-screen is an example of how to use .move_to_end() with a key argument and relying on the default value of last.

02:12 When you call .move_to_end() with a key as an argument, you move the key-value pair at hand to the end of the dictionary. That’s why ('one', 1) is now in the last position.

02:22 Note that the rest of the items remain in the original order. If you pass False to last, then you’re moving the item to the beginning.

02:35 Here, you move ('one', 1) back to the beginning of the dictionary. This provides an interesting and powerful feature. For example, with .move_to_end(), you can sort an ordered dictionary by keys.

02:52 Here, you first create an ordered dictionary, letters. The for loop iterates over the sorted keys and moves every item to the end of the dictionary.

03:03 When the loop finishes, the ordered dictionary has its items sorted by keys.

03:12 Sorting the dictionary by values would be an interesting exercise, so let’s take a look at one way you could achieve this.

03:32 Here, a lambda expression is used to provide the key, giving access to the value of each item.

03:45 As you can see, the dictionary is now ordered by the value of the key. If you want to learn more about lambda expressions, check out this Real Python course, which covers them in detail.

04:00 Another interesting feature of OrderedDict is its enhanced version of .popitem(). By default, .popitem() removes and returns an item in last-in/first-out order.

04:12 In other words, it removes items from the right end of the ordered dictionary.

04:23 Here, you remove all the items from numbers using .popitem(). Every call to this method removes a single item from the end of the underlying dictionary. If you call .popitem() on an empty dictionary, then you get a KeyError. Up to this point, .popitem() behaves the same as in regular dictionaries.

04:45 In OrderedDict, however, .popitem() also accepts a Boolean argument called last, which defaults to True.

04:52 If you set last to False, then .popitem() removes the items in first-in/first-out order. In other words, it removes items from the beginning of the dictionary.

05:22 Once again, the last call to .popitem() raises a KeyError because the underlying dictionary is already empty. In the next section of the course, you’ll deepen your knowledge of OrderedDict by looking at how to test for equality between dictionaries.

Become a Member to join the conversation.