Created
October 3, 2013 03:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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