ChainMap
  • groups multiple dicts or other mappings together to create a single, updateable view
  • Initialization
    from collections import ChainMap
    
    a = {'Name': 'Lin', 'Age': 39}
    b = {'maker': 'Honda', 'year': 2016}
    cm = ChainMap(a, b) # ChainMap({'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016})
    
    # a list of maps
    cm.maps # [{'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016}]
    
    # add a new map
    cm.maps.append({'ID': 970196897}) # ChainMap({'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016}, {'ID': 970196897})
    
    # create a copy of chainmap and add a new map in at the front
    cm2 = cm.new_child()
    print(cm2.maps) # [{}, {'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016}, {'ID': 970196897}]
    
    # fill the new map
    cm2.maps[0] = {'Color': 'Blue'} # ChainMap({'Color': 'Blue'}, {'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016}, {'ID': 970196897})
    
    # get parent
    p = cm2.parents # ChainMap({'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016}, {'ID': 970196897})
    
    print(id(cm), id(cm2), id(p)) # 140454428292816 140454428291904 140454428443168
                
    Access
    cm['Name'] # Lin
    cm['maker'] # Honda
    
    # keys()
    list(cm.keys()) # ['maker', 'year', 'Name', 'Age']
    
    # values()
    list(cm.values()) # ['Honda', 2016, 'Lin', 39]
    
    # items()
    for k, v in cm.items():
        print(k, v)
    
    cm['maker'] = 'Subaru'
                
    Update
    a = {'Name': 'Lin', 'Age': 39}
    b = {'maker': 'Honda', 'year': 2016}
    cm = ChainMap(a, b)
    
    a['Age'] = 42
    print(cm) # ChainMap({'Name': 'Lin', 'Age': 42}, {'maker': 'Honda', 'year': 2016})
    
    cm.maps[1]['maker'] = 'Subaru'
    print(cm) # ChainMap({'Name': 'Lin', 'Age': 39}, {'maker': 'Subaru', 'year': 2016})
    
    cm['maker'] = 'Subaru'
    print(a) # {'Name': 'Lin', 'Age': 39, 'maker': 'Subaru'}
    print(cm) # ChainMap({'Name': 'Lin', 'Age': 39, 'maker': 'Subaru'}, {'maker': 'Subaru', 'year': 2016})
                
    Built-in Functions
    a = {'Name': 'Lin', 'Age': 39}
    b = {'maker': 'Honda', 'year': 2016}
    cm = ChainMap(a, b) # ChainMap({'Name': 'Lin', 'Age': 39}, {'maker': 'Honda', 'year': 2016})
    
    list(cm) # all nested keys, ['maker', 'year', 'Name', 'Age']
    
    print('Name' in cm) # True
    
    len(cm) # 4
    
    dict(cm) # Flatten into a regular dictionary, {'maker': 'Honda', 'year': 2016, 'Name': 'Lin', 'Age': 39}
                
    Reference
  • Python 3 Standard Library
  • Python 3 Module of the Week