Skip to content

Instantly share code, notes, and snippets.

@mgaitan
Created October 3, 2013 03:36
Show Gist options
  • Select an option

  • Save mgaitan/6804513 to your computer and use it in GitHub Desktop.

Select an option

Save mgaitan/6804513 to your computer and use it in GitHub Desktop.
return a concatenation of dictionaries ``a`` and ``b`` prefering values of b but extending lists
def smart_update(a, b):
"""
return a concatenation of dictionaries ``a`` and ``b``
prefering values of b but extending lists
>>> a = {'name': 'Martin', 'things': [1]}
>>> b = {'name': 'Nati', 'things': [2]}
>>> smart_update(a, b)
{'name': 'Nati', 'cosas': [1, 2]}
"""
r = a.copy()
r.update(b)
for (k, v) in a.items():
if isinstance(v, list) and isinstance(b.get(k, None), list):
r[k] = v + b[k]
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment