Choosing a Dictionary
All the Python dictionary implementations listed in this course are valid implementations that are built into the Python standard library.
If you’re looking for a general recommendation on which mapping type to use in your programs, I’d point you to the built-in dict
data type. It’s a versatile and optimized hash table implementation that’s built directly into the core language.
I would recommend that you use one of the other data types listed here only if you have special requirements that go beyond what’s provided by dict
. All the implementations are valid options, but your code will be clearer and easier to maintain if it relies on standard Python dictionaries most of the time.
Here are resources for documentation about dictionaries:
- Built-in Types:Mapping types – dict | Python Documentation
- collections – Container Datatypes: OrderedDict | Python Documentation
- collections – Container Datatypes: defaultdict | Python Documentation
- collections – Container Datatypes: ChainMap | Python Documentation
- types – Dynamic type creation and names for built-in types: MappingProxyType | Python Documentation
- Glossary: hashable | Python Documentation
Here are additional Real Python resources about dictionaries:
00:00
In the previous lesson, I discussed the ChainMap
class from the collections
library and the MappingProxyType
from the types
library. In this lesson, I’m going to wrap up the section on dictionaries, help you decide how to choose which implementation to use when, and give you some pointers for further investigation.
00:19
This section has been all about dictionaries, also known as hashmaps. You’ve seen the built-in dict
, OrderedDict
, defaultdict
, ChainMap
, and the MappingProxyType
—all with slightly different purposes.
00:33
So, how do you choose between them? Well, it’s a fairly easy answer. Start with the built-in dict
. Unless you’re doing something specific, it’s probably going to do most of what you need. Particularly now, if you’re using a more recent version of Python and that built-in dictionary is ordered by default, the need for OrderedDict
isn’t really as pressing as it used to be. That being said, if you need to be backward compatible with older versions of Python or you’re doing something tricky with the dictionary where you want to make sure that it’s very obvious that the ordering is important, you might explicitly use the OrderedDict
so that someone else reading your code knows what your intent is. Of course, there’s also those handy extra methods that the OrderedDict
supports.
01:19 If you’re needing to use those for your use case, that might be why you might change your mind here.
01:25
Any of the other classes that I described really should only be used for specific use cases. As I mentioned in the lesson on defaultdict
, the most common usage for the defaultdict
is when you need a dictionary that contains values that are lists. Rather than having special code that instantiates a new key with an empty list, defaultdict
can take care of that for you.
01:48
The ChainMap
is a useful utility for managing multiple dictionaries. It easily allows you to search across those different dictionaries for the first instance of a particular key.
01:59
Because insertion is based on the first dictionary, I would avoid making changes to the ChainMap
. If you need to make changes to a dictionary, change the dictionary itself, and then have the ChainMap
reflect this. It’ll make your code easier to read.
02:14
And finally, I talked about the MappingProxyType
out of the types
library. This is a very specific use case where you need to have a read-only dictionary. This object isn’t very common.
02:26 I’ve never actually seen it in the field, myself. Anyone who comes across it is probably going to have to look it up. Unless you really need that read-only dictionary, you might want to stick with just the built-in one.
02:38
If you’re looking for more information about any of these, the Python documentation has all sorts of stuff for you. Here’s the link for the dict
itself, the OrderedDict
, the defaultdict
, the ChainMap
, and the MappingProxyType
.
02:52 All of these links will be presented in the description below this lesson. If you want to better understand what is and isn’t hashable, the glossary section of the Python documentation talks about this. And finally, if you’d like to dig more into how dictionaries are used in the implementation of Python, this article talks about how attributes are implemented as dictionaries inside of the actual classes.
03:19 That’s it for this section. The next section is on arrays.
Become a Member to join the conversation.