How to Use ChainMap
00:00
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.
00:13
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.
00:21
So, it’s from this collections
module, as I told you already, and we’re going to import this ChainMap
. And 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.
00:38
This 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.
00:54
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()
01:07
of fruits
and 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.
01:21
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.
01:43
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 .items()
and .keys()
and .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 .keys()
, .values()
, and .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.
02:18
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 1
.
02:30
Right? So, what happens if we want to make a ChainMap
of these? And we’ll call it a_chain = ChainMap(a, a2)
.
02:41
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 a_chain
at 'a'
?
02:56
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.
03:07
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.
03:19
Very useful. Very cool. I hope you enjoyed this video learning a little about ChainMap
.
Ricky White RP Team on Feb. 28, 2020
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.