ChainMap and MappingProxyType
In the previous lesson, I discussed the
defaultdict classes from the
collections library. In this lesson, I’m going to talk about
ChainMap and the
Like the classes in the previous lesson,
ChainMap is also in the
collections library. The purpose of the
ChainMap is to group multiple dictionaries together into a single mapping.
00:23 This gives you the ability to provide search for a key across all of the dictionaries inside of the map. To demonstrate the use of the chain, I’m going to need a couple of dictionaries, so let me just quickly create them.
And now that I have the dictionaries, I’m going to import
ChainMap from the
collections library. To create a chain, I instantiate the
ChainMap, passing in the dictionaries that I wish to chain together.
01:07 And here it is. You can see the contents of the two dictionaries that are chained. Now I access things inside of the chain the same way I would a dictionary.
Here’s the response for the key for the Beagle, and for the Atlas rocket, and finally for the Enterprise. Notice that there is an
"Enterprise" key in both the
chain responds back with the first key in the order of the dictionaries that it can find, so the response to the
"Enterprise" key is for the aircraft carrier rather than for the space shuttle.
Just like a dictionary, if you try to access a key that doesn’t exist, you’ll get a
KeyError. The chain holds references to the dictionaries.
Anything you do to the chain will impact the dictionaries that are associated with it. For example, let me delete the key
"Beagle". I’ve removed the key
"Beagle" from the
chain, and if you look at the
ships dictionary, it’s gone from there as well. In a similar fashion, let me add a new key to the
02:25 There’s your rocket. And unfortunately, this is where chains are a little problematic. Any additions to the chain get put into the first dictionary associated with the chain, so now I’ve got my Ariane rocket inside the wrong dictionary.
02:44 Generally, I would recommend just using the chains to manage reading keys from multiple dictionaries, and if you’re going to do insertions or deletions, it’s safer to do them directly to the dictionaries that were chained together, rather than to the chain itself.
You’re less likely to shoot yourself in the foot in the code. Stepping away from the
collections library and into the
types library, you can find the
03:09 This is a way of getting a read-only wrapper to a dictionary. This was added in Python 3.3, so if you’re using a later version, you’re not going to be able to get at this particular object.
Let me just quickly create a dictionary. And now I’m going to import the
To create the read-only dictionary, you instantiate the
MappingProxyType, handing it the dictionary that is writeable.
Like a normal dictionary, I can access the keys inside of
read_only, but unlike a normal dictionary, if I try to do assignment, I’ll get an error.
03:58 It doesn’t let me change the key.
04:03 It also doesn’t let me add a key. The map is a map to the reference, though, so if I do change the original, the proxy does get updated.
And there it is.
'three' is inside of the proxy. As you’ve seen, there’s a variety of different types of dictionaries and maps inside of Python, besides just the built-in one. In the next lesson, I’ll talk about how to choose between them and give you some references for digging in further.
Become a Member to join the conversation.