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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

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.

webdeveloper on Feb. 28, 2020

Hi, It is awesome. How can I get this data structure in Json format.

[
  {
    "SectionName": "Header",
    "SectionOrder": 0,
    "Fields": [
      {
        "ID": 0,
        "Order": -12,
        "Name": "ID",
        "Display": "ID",
        "Visible": true,
        "fieldType": null,
        "Width": null
      },
      {
        "ID": 0,
        "Order": -11,
        "Name": "FormSubmissionDate",
        "Display": "Submission Date",
        "Visible": true,
        "fieldType": "DatePicker",
        "Width": null
      },
      {
        "ID": 0,
        "Order": -10,
        "Name": "Site",
        "Display": "Site",
        "Visible": true,
        "fieldType": null,
        "Width": null
      },
      {
        "ID": 0,
        "Order": -9,
        "Name": "JobNumber",
        "Display": "Job Number",
        "Visible": true,
        "fieldType": null,
        "Width": null
      },
      {
        "ID": 0,
        "Order": -8,
        "Name": "SiteLevel",
        "Display": "Level",
        "Visible": true,
        "fieldType": null,
        "Width": null
      },
      {
        "ID": 0,
        "Order": -7,
        "Name": "Workplace",
        "Display": "Workplace",
        "Visible": true,
        "fieldType": null,
        "Width": null
      },
      {
        "ID": 0,
        "Order": -6,
        "Name": "SubmittedBy",
        "Display": "Submitted By",
        "Visible": true,
        "fieldType": null,
        "Width": null
      }
    ]
  },
  {
    "SectionName": "Notes",
    "SectionOrder": 1,
    "Fields": [
      {
        "ID": 17756,
        "Order": -5,
        "Name": "comments",
        "Display": "Comments",
        "Visible": true,
        "fieldType": "MultiLineEntry",
        "Width": null
      },
      {
        "ID": 17757,
        "Order": -4,
        "Name": "workers_present",
        "Display": "Workers Present",
        "Visible": true,
        "fieldType": "GlobalListPicker",
        "Width": null
      },
      {
        "ID": 17758,
        "Order": -3,
        "Name": "multiphoto_picker_1",
        "Display": "Pictures",
        "Visible": true,
        "fieldType": "MultiPhotoPicker",
        "Width": null
      }
    ]
  },
  {
    "SectionName": "Distribution",
    "SectionOrder": 2,
    "Fields": [
      {
        "ID": 17790,
        "Order": -2,
        "Name": "Report_Distribution1",
        "Display": "Report Distribution",
        "Visible": true,
        "fieldType": "GlobalListPicker",
        "Width": null
      }
    ]
  }
]

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.