The collections module in the standard library provides the ChainMap class which joins multiple dictionaries to act as if it was a one large dictionary.
Consider the following example:
See More:
Consider the following example:
#import the ChainMap class from collections import ChainMap d1 = {'one': 1, 'two': 2, 'three': 3} d2 = {'four': 4, 'five': 5, 'six': 6} d3 = {'seven': 7, 'eight': 8, 'nine': 9} chain = ChainMap(d1, d2, d3) print(chain)
Output:ChainMap({'one': 1, 'two': 2, 'three': 3}, {'four': 4, 'five': 5, 'six': 6}, {'seven': 7, 'eight': 8, 'nine': 9})
The resulting ChainMap object has a dictionary interface. When retrieving data, each dictionary is searched in turn, going from the first to the last.#import the ChainMap class from collections import ChainMap d1 = {'one': 1, 'two': 2, 'three': 3} d2 = {'four': 4, 'five': 5, 'six': 6} d3 = {'seven': 7, 'eight': 8, 'nine': 9} chain = ChainMap(d1, d2, d3) print(chain['three']) print(chain['five']) print(chain['eight']) print(chain['nine'])
Output:3
5
8
9
As shown chainmap object acts as if it was a one long dictionary made from the individual dictionaries.See More: