How to Use ChainMap
Hi, and welcome. In this video, we’re going to look at another cool Python dictionary feature, which is called
ChainMap. It’s from the
collections library in the standard library of Python.
So, let’s get into it, and we’ll talk about why you might be interested in this
ChainMap thing and why it’s kind of fun to use and cool to look at.
So, it’s from this
collections module, as I told you already, and we’re going to import this
collections, in general, is just a really useful library of awesome stuff, all to do with some kind of object collection, as you might gather from the name.
ChainMap is a dictionary-like class. It’s not in fact a variant of a dictionary, but it is dictionary-like. It allows you to chain together multiple dictionaries into one big iterating thing. Let’s take a look at how it works.
We’re going say our
chained_dict is—well, it’s not really a dict, so let’s call it just our
chained_map = ChainMap()
veggies. It takes in dictionaries. And then let’s take a look at it, what is it? Well, it’s a
ChainMap, and as you can see it has these dictionaries within it, and it prints them out when you try to print it.
But the cool thing is that you can iterate through it just like you can iterate through a dictionary. And you can print out
key and then
chained_map at that
key, and you can get all of these dictionary items all at once without having to worry about saying, you know,
for key in fruits:
for key in veggies: and then print out both of them in a row in multiple loops.
You can just do one big loop with this
ChainMap. So it’s pretty cool, and it has also—which is great—it has the same
.values() functions that the dictionary has natively. And so you can do all of these same different things with it, which is pretty sweet. So, as I said, it implements
.items(), and that’s really important.
02:07 One thing that I want to go over real quick is just to take a look at what happens if there are multiple, if both dictionaries you want to add have the same key.
So let’s say we have a dictionary called
a, which just maps the letter
'a' to the value
100. And then let’s say we have
a2 and it just maps the letter
'a' to the value
Right? So, what happens if we want to make a
ChainMap of these? And we’ll call it
a_chain = ChainMap(a, a2).
And then we print out
a_chain. What does it have? Well, it has both of these keys in it, but clearly they’re the same key. So what happens if you try to get
I’m sorry, I forgot my thing there. So as you can tell, it has
100 as the value for
'a'. And so it’s going to get the first thing which maps there.
So that’s a really important thing to note just for if you want to use this in real life. So,
ChainMap. Essentially, it’s like a big dictionary which combines other dictionaries into one giant iterable class.
Very useful. Very cool. I hope you enjoyed this video learning a little about
Hi webdeveloper. If you want to learn how to serialize data to JSON, check out our course on it here: realpython.com/courses/working-json-data-python/
Become a Member to join the conversation.
webdeveloper on Feb. 28, 2020
Hi, It is awesome. How can I get this data structure in Json format.