Since Python 3.3 You can chain dictionaries which contain the same key in a prioritized order:
from collections import ChainMap prio_1 = {"param_1": "foo"} prio_2 = {"param_1": "foobar", "param_2": "bar"} combined = ChainMap(prio_1, prio_2) print(combined["param_1"]) # outputs 'foo' print(combined["param_2"]) # outputs 'bar'
The param_1 from the prio_1 dictionary is dominant, so it isn’t overwritten by the key-value pair from prio_2 dict. prio_2 dict just adds the param_2 because it is not yet in the ChainMap.
The nice thing is that no information is lost, the ChainMap object contains both dictionaries.
You can use it for example in a situation where you want to chain arguments from argparse, os.environ and default parameters. See here
Python 2
If you are still using Python 2 there is a backport for you
pip install chainmap
from chainmap import ChainMap