Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

ChainMap

ChainMap creates one dictionary-like view over multiple mappings. Lookups use the first matching mapping, while mutations target only the first mapping.

How it works

ChainMap presents several mappings as one dictionary-like view. Lookups inspect them from first to last, so earlier mappings take precedence; mutations affect only the first mapping, while all mappings remain linked by reference.

Syntax

class collections.ChainMap(*maps)

Parameters

NameTypeDescription
*mapsmappingMappings to combine, searched from first to last; if omitted, an empty mapping is used.

Returns

A ChainMap object providing a single view over the supplied mappings.

Key points

  • It combines dictionaries or other mapping objects into one dictionary-like view that can be updated.
  • Calling ChainMap() creates a chain backed by an empty dictionary rather than an empty list of mappings.
  • The component mappings are maintained as an ordered list, available through the maps attribute.
  • Key lookup proceeds from the first mapping toward the last and stops at the first mapping containing the key.
  • For a live layered view, ChainMap can be faster than constructing a separate dictionary through repeated updates.

Common mistakes

  • Assignments, updates, and deletions do not search for the mapping containing a key; they change only the first mapping.
  • Changes made directly to any underlying mapping are visible through the ChainMap, and it does not copy their contents.

Example: Layer configuration mappings

from collections import ChainMap

base = {"timeout": 30, "retries": 2}
overrides = {"timeout": 10}
config = ChainMap(overrides, base)

print(config["timeout"])
print(config["retries"])
config["retries"] = 3
print(base["retries"])

The override mapping wins for lookups, while assignment changes only that first mapping.

When to use it

Layered configuration

Layer command-line or request-specific settings over application defaults without copying the dictionaries.

Nested scopes

Represent local variables followed by enclosing variables in a nested-scope model.

Template context

Provide template values with progressively broader fallback mappings.

Interview questions

QHow does ChainMap resolve lookups and mutations?

ChainMap combines multiple mappings into one view without merging them into a new dictionary. A lookup checks mappings in order and returns the first matching key; changes are applied only to the first mapping.

Follow-up: How would you model a nested scope? Place the innermost scope first and outer scopes afterward, then use new_child() or parents to enter or leave levels.

QWhat happens when ChainMap is created with no arguments?

When constructed without mappings, ChainMap supplies one empty dictionary, ensuring the chain always has an underlying mapping. Its mappings are held in the public maps list.

Follow-up: What risk follows from exposing maps? Code can reorder or replace the underlying mappings, changing lookup precedence and mutation targets.

QWhy use ChainMap instead of repeatedly updating a new dictionary?

ChainMap is useful when several mappings should remain separate but be viewed together, such as layered configuration or nested scopes. It can avoid repeatedly building a merged dictionary with update calls.

Follow-up: When should a copied dictionary be preferred? Use one when an independent snapshot is required rather than a live, reference-based view.

  • collections.ChainMap.maps
  • collections.ChainMap.new_child()
  • collections.ChainMap.parents
  • collections.ChainMap.keys()
  • collections.ChainMap.values()
  • collections.ChainMap.items()

Source: collections — Container datatypes — Python 3.14.6 documentation. Checked 2026-10-16.