Search this site


Metadata

Articles

Projects

Presentations

Python's dict objects can be merged

Much to my surprise, python dict objects can be merged. I've needed this a few times but never knew about it mostly due to 'pydoc dict' not mentioning 'merge' anywhere.
>>> a = { 1: 2, 3: 4 }
>>> b = { 1: 100, 5: 6 }
>>> b.update(a)
>>> print b
{1: 2, 3: 4, 5: 6}
dict.update() takes any number of arguments, all of which must be dict objects. This lets you merge serveral dicts into one. Conflict resolution, it seems, is "last one in wins."