ID EN
Collections

ChainMap

Python 3.11

A ChainMap groups multiple dicts or other mappings together to create a single, updateable view. If no maps are specified, a single empty dictionary is provided so that a new chain always has at least one mapping.

Syntax

PYTHON
class collections.ChainMap(*maps)

Examples

Example 1
PYTHON
>>> baseline = {'music': 'bach', 'art': 'rembrandt'}
>>> adjustments = {'art': 'van gogh', 'opera': 'carmen'}
>>> list(ChainMap(adjustments, baseline))
['music', 'art', 'opera']
Example 2
PYTHON
>>> combined = baseline.copy()
>>> combined.update(adjustments)
>>> list(combined)
['music', 'art', 'opera']

See Also

ChainMap ChainMap ChainMap ChainMap ChainMap nonlocal nested scopes super()