def rec_merge(d1, d2):
for k, v in d1.items():<EOL><INDENT>if k in d2:<EOL><INDENT>if all(isinstance(e, collections.MutableMapping) for e in (v, d2[k])):<EOL><INDENT>d2[k] = rec_merge(v, d2[k])<EOL><DEDENT><DEDENT><DEDENT>d3 = d1.copy()<EOL>d3.update(d2)<EOL>return d3<EOL>
Recursively merge two dictionaries

    Update two dicts of dicts recursively,
    if either mapping has leaves that are non-dicts,
    the second's leaf overwrites the first's.

    import collections
    import functools

    e.g. functools.reduce(rec_merge, (d1, d2, d3, d4))