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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

ChainMap and MappingProxyType

00:00 In the previous lesson, I discussed the OrderedDict and defaultdict classes from the collections library. In this lesson, I’m going to talk about ChainMap and the MappingProxyType.

00:12 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.

00:47 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.

01:19 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 ships and space_ships dictionaries. 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.

01:52 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.

02:00 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 chain.

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.

02:58 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 MappingProxyType.

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.

03:22 Let me just quickly create a dictionary. And now I’m going to import the MappingProxyType.

03:33 To create the read-only dictionary, you instantiate the MappingProxyType, handing it the dictionary that is writeable.

03:46 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.

04:17 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.