Deep merge two dicts?

Ian Kelly ian.g.kelly at gmail.com
Fri Apr 13 12:50:15 EDT 2012


On Fri, Apr 13, 2012 at 5:11 AM, John O'Hagan <research at johnohagan.com> wrote:
> I think you also have to check if a[k] is a dict before making the recursive
> call, else for example dmerge({'a': 1}, {'a': {'b': 1}}) fails with a
> TypeError. In that case the third line above should read:
>
>    if k in a and isinstance(a[k], dict) and isinstance(v, dict):

Okay, but then what do you do in that case?  You can't merge a dict
into an int.  Unless the OP has some specific type conflict semantics
in mind, the above *should* raise a TypeError, because in the above
you have passed in two structures that are incompatible for merging.



More information about the Python-list mailing list