Breadcrumbs

Snippets and articles I want to find later; a breadcrumb trail trough the web.
Jun 28
Permalink

Stupid Python tricks: diff for dicts

def dict_diff(a, b):
    diffs = {}
    for k in a:
        if k in b and isinstance(a[k], dict) 
and isinstance(b[k], dict): dd = dict_diff(a[k], b[k]) if dd: diffs[k] = dd elif k not in b or a[k] != b[k]: diffs[k] = a[k] return diffs def dict_symmetric_diff(a, b): diffs = {} for k in set(a.keys()) & set(b.keys()): if isinstance(a[k], dict) and isinstance(b[k], dict): dsd = dict_symmetric_diff(a[k], b[k]) if dsd: diffs[k] = dsd elif a[k] != b[k]: diffs[k] = a[k] for k in set(a.keys()) ^ set(b.keys()): diffs[k] = a[k] if k in a else b[k] return diffs >>> a = {'x': {'c': 15, 'd': 10}, 'y':{}}
>>> b = {'x': {'c': 15, 'd': 15, 'e':2}, 'z':{'l':0}}
>>> dict_diff(a, b)
{'x': {'d': 10}, 'y': {}}
>>> dict_diff(b, a)
{'x': {'d': 15, 'e': 2}, 'z': {'l': 0}}
>>> dict_symmetric_diff(a, b)
{'x': {'d': 10, 'e': 2}, 'y': {}, 'z': {'l': 0}}
>>> dict_symmetric_diff(b, a)
{'x': {'d': 15, 'e': 2}, 'y': {}, 'z': {'l': 0}}