2025-04-24 18:16:11 +05:30

24 lines
799 B
Python

def merge(source: dict, destination: dict):
"""
Merge source dictionary into destination dictionary recursively in place.
Examples
# >>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } }
# >>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } }
# >>> merge(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } }
True
Args:
source (dict): Source dictionary
destination (dict): Destination dictionary
"""
for key, value in source.items():
if isinstance(value, dict):
# get node or create one
node = destination.setdefault(key, {})
merge(value, node)
else:
destination[key] = value
return destination