Merge two objects in Python

Chris Dutrow picture Chris Dutrow · Feb 12, 2013 · Viewed 41.1k times · Source

Is there a good way to merge two objects in Python? Like a built-in method or fundamental library call?

Right now I have this, but it seems like something that shouldn't have to be done manually:

def add_obj(obj, add_obj):

    for property in add_obj:
        obj[property] = add_obj[property]

Note: By "object", I mean a "dictionary": obj = {}

Answer

phihag picture phihag · Feb 12, 2013

If obj is a dictionary, use its update function:

obj.update(add_obj)