OrderedDict and defaultdict
In the previous lesson, I concentrated on the
dict type, which is built into Python. In this lesson, I’m going to cover
defaultdict, which are part of the
collections standard library.
dict type isn’t the only kind of dictionary that Python provides. There are others in the standard library.
OrderedDict, part of the
collections library, remembers the insertion order of the keys.
It also provides some other useful functionality as well. It supports iteration over the dictionary using Python’s built-in
reversed() method, and then provides two additional methods that the standard dictionary doesn’t have.
.move_to_end() moves a key inside of
OrderedDict to the end of the listing, as if it was the last thing inserted.
.popitem()—although that’s supported by the standard dictionary type, the
OrderedDict type takes an additional parameter, allowing you to pop either the first item or the last item off the
Depending on how closely you’ve been following along with the changes to the dictionary in CPython implementations,
OrderedDict might be a bit of a surprise that it exists. And that’s because as of Python 3.6,
dict type itself maintained order.
It did that as part of how it was implemented but wasn’t part of the language specification. Python 3.7 changed the specification so that maintaining the order was a requirement of the
Support for working with the built-in
reversed() function was added to
dict in Python 3.8, so if you’re using Python 3.8 or 3.9, the built-in
dict type is very, very similar to
They haven’t removed
OrderedDict in order to maintain backward compatibility, but for the most part, you no longer need to use it explicitly.
Although almost everything that I’m about to show you is possible with the built-in
dict type in Python 3.9—the version of the language I’m using—I’m going to explicitly use
collections in case you’re using an older version and need to follow along.
02:07 Once you have it imported, you can create it by instantiating the class.
The constructor for the
OrderedDict class takes an arbitrary number of arguments. It automatically creates a dictionary mapping the keys to the values of those arguments passed in. And there’s the repr for it.
Notice that it displays it like a list, showing that
3 were inserted, in that order. Like a regular dictionary, you can use the square bracket notation to insert items into the dictionary.
4 has been appended to the end.
02:48 And notice, although I’ve been putting numbers in here for clarity’s sake, that has nothing to do with the order in the dictionary. It’s not sorting—it’s based on the insertion order.
Similar to the built-in
OrderedDict supports the
.keys() method. And the
reversed() function is also supported on the dictionary. This returns an iterator, which, if I turn into a list, shows you the value of the keys in reverse of insertion order.
One of the differences between the
OrderedDict and the built-in type is the support for the
03:30 This tells the class to take the indicated key and move it to the end of the ordering in the list, thus restoring my numeric order of my dictionary.
Just like a regular dictionary,
OrderedDict supports the
.popitem() method. This pulls the last item off and returns it. The dictionary now does not contain the key
Unlike the regular dictionary type,
OrderedDict also supports a parameter to
.popitem(). If you pass in
False, it will pop the first item off the list instead of the last one.
And there I’ve pulled the first key off the dictionary listing. There’s lots of great stuff inside of the
collections library. The next one I’m going to show you is the
04:18 This is a dictionary that allows you to specify the behavior of what happens when a key that is called is missing. A common use case for this is when you need a dictionary that contains lists.
04:30 By using a default dictionary configured with an empty list, the first use of an empty key is initialized to an empty list. This means you don’t have to write the additional code checking whether or not the key exists in the dictionary, and if it doesn’t, add the key to the dictionary with an empty list and then add the item that you were intending to put in in the first place.
04:52 Let me just import that.
When you construct a
defaultdict, you pass in what the default is for the dictionary. In this case, I’m passing in the
animals object is now a
defaultdict that uses a
list as the default value if the keys are empty. The REPL shows you that the dictionary is currently empty.
Referencing the key
"dogs" returns an empty list. This is what
defaultdict does for you. Now if you look at
animals again, you’ll notice that the key
'dogs' is now inside of the
05:38 You can now directly address this list and append items to it.
You can even append items to a key that doesn’t exist. The first reference will create the empty list and the
.append() will work on that empty list. Here’s the resulting dictionary.
It now contains two keys—one for
'dogs', one for
'cats', both of which contain lists,
Let me just add one more thing to
"dogs" here, and then you can reference the key specifically and get the list resulting. You can initialize your
defaultdict with just about anything, although the list is the most common thing I’ve seen out there in code.
That’s all for now. In the next lesson, I’ll show you the
ChainMap and the
Become a Member to join the conversation.