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.

Getting Started With OrderedDict

00:00 Getting Started With OrderedDict. Python’s OrderedDict is a dictionary subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary.

00:12 When you iterate over an OrderedDict object, items are traversed in the original order. When you update the value of an existing key, then the order remains unchanged.

00:22 If you remove an item and reinsert it, then the item is being added at the end of the dictionary. Being a dictionary subclass means that it inherits all the methods a regular dictionary provides.

00:34 OrderedDict also has additional features that you’ll learn about in this course. In this section, you’ll learn the basics of creating and using OrderedDict objects in your code.

00:45 Unlike standard dictionaries, OrderedDict isn’t a built-in type, so the first step to create an OrderedDict object is to import the class from collections. There are several ways to create ordered dictionaries, and most of them are identical to how you create a regular dictionary object.

01:04 For example, you can create an empty ordered dictionary by instantiating the class without arguments. First, you import OrderedDict from collections.

01:18 Then you create an empty ordered dictionary by instantiating OrderedDict without providing arguments to the constructor. You can add key-value pairs to the dictionary by providing a key in square brackets ([]) and assigning a value to that key.

01:41 When you reference numbers, you get an iterable of key-value pairs that holds items in the same order they were inserted into the dictionary.

01:55 You can also pass an iterable of items as an argument to the constructor of OrderedDict.

02:05 When you use a sequence, such as a list or tuple, the order of the items in the resulting ordered dictionary matches the original order of items in the input sequence.

02:17 If you use a set, then the final order of items is unknown until the OrderedDict is created.

02:27 If you use a regular dictionary as an initializer to an OrderedDict object, and you’re on Python 3.6 or beyond, then you get the behavior seen on-screen.

02:42 The order of items in the OrderedDict objects matches the order in the original dictionary. On the other hand, if you’re using a version of Python lower than 3.6, then the order of items is unknown.

02:59 Since dictionaries in Python 3.5 don’t remember the order of their items, you don’t know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained.

03:15 You can also create an ordered dictionary by passing keyword arguments to the class constructor.

03:28 Since Python 3.6, functions retain the order of keyword arguments passed in a call, so the order of the items in the OrderedDict matches the order in which you passed the keyword arguments to the constructor. In earlier Python versions, that order is unknown. Finally, OrderedDict also provides the .fromkeys() method, which creates a new dictionary from an iterable of keys and sets all its values to a common value.

03:58 Here, you create an ordered dictionary using a list of keys as a starting point. The second argument to .fromkeys() provides a single value, 0, to all the items in the dictionary.

04:12 Since OrderedDict is a mutable data structure, you can perform mutating operations on its instances. You can insert new items, update and remove existing items, and so on.

04:26 If you insert a new item into an existing ordered dictionary, then the item is added to the end of the dictionary.

04:47 The newly added item, ('four', 4), is placed at the end of the underlying dictionary, so the order of the existing items remains unaffected, and the dictionary keeps the insertion order.

04:57 If you delete an item from an existing ordered dictionary and insert that same item again, then the new instance of the item is placed at the end of the dictionary.

05:07 If you remove the ('one', 1) item and insert a new instance of the same item, then the new item is added to the end of the underlying dictionary.

05:24 If you reassign or update the value of an existing key-value pair in an ordered dictionary object, then the key maintains its position but gets a new value.

05:38 If you update the value of a given key in an ordered dictionary, then the key isn’t moved, but assigned the new value in place. In the same way, if you use .update() to modify the value of an existing key-value pair, then the dictionary remembers the position of the key and assigns the updated value to it.

06:02 In the next section of the course, you’ll deepen your knowledge of OrderedDict by looking at iteration.

Become a Member to join the conversation.