Python ChainMap

jjw's avatar
Published Oct 15, 2025
Contribute to Docs

The ChainMap class from Python’s collections module provides a single, combined view of multiple dictionaries or mappings, searching them in order and returning the first match for each key.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ChainMap(*maps)

Parameters:

  • *maps: Variable number of mapping objects (dictionaries, etc.) to be chained together.

Return value:

Returns a ChainMap object that provides dictionary-like access across all supplied mappings, resolving keys from the first mapping where they appear.

Example

This example demonstrates how to create and use a ChainMap:

from collections import ChainMap
# Create multiple dictionaries
default_settings = {'theme': 'dark', 'language': 'en', 'notifications': True}
user_settings = {'theme': 'light', 'font_size': 14}
session_settings = {'language': 'es'}
# Create a ChainMap with the dictionaries
settings = ChainMap(session_settings, user_settings, default_settings)
# Access values (searches from left to right)
print(settings['theme'])
print(settings['language'])
print(settings['notifications'])
print(settings['font_size'])

The output of this code is:

light
es
True
14

This example shows how ChainMap searches through the dictionaries in order, returning the first occurrence of each key.

Codebyte Example

This codebyte example demonstrates practical usage of ChainMap for configuration management:

Code
Output
Loading...

Key Features of ChainMap

  • Search Order: Keys are searched from left to right across the provided mappings.
  • First Match Wins: The first occurrence of a key is returned.
  • Dynamic Updates: Changes to the underlying dictionaries are reflected in the ChainMap.
  • Memory Efficient: Doesn’t create copies of the data, just references to the original mappings.

Common Use Cases of ChainMap

  • Configuration Management: Combining system defaults, user preferences, and environment-specific settings.
  • API Response Merging: Combining multiple API responses into a single view.
  • Template Systems: Merging multiple context dictionaries for rendering templates.
  • Fallback Mechanisms: Providing fallback values when primary sources don’t have certain keys.

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours