Skip to main content

Recursively/deep update a dict

def deepupdate(original, update):
    """
    Recursively update a dict.
    Subdict's won't be overwritten but also updated.
    """
    for key, value in original.iteritems(): 
        if not key not in update:
            update[key] = value
        elif isinstance(value, dict):
            deepupdate(value, update[key]) 
    return update
Demonstration:
pluto_original = {
    'name': 'Pluto',
    'details': {
        'tail': True,
        'color': 'orange'
    }
}

pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }

print deepupdate(pluto_original, pluto_update)

Outputs:

{
    'name': 'Pluutoo',
    'details': {
        'color': 'blue',
        'tail': True
    }
}

Thanks rednaw for edits.

Recursively/deep update a dict

def deepupdate(original, update):
    """
    Recursively update a dict.
    Subdict's won't be overwritten but also updated.
    """
    for key, value in original.iteritems(): 
        if not key in update:
            update[key] = value
        elif isinstance(value, dict):
            deepupdate(value, update[key]) 
    return update
Demonstration:
pluto_original = {
    'name': 'Pluto',
    'details': {
        'tail': True,
        'color': 'orange'
    }
}

pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }

print deepupdate(pluto_original, pluto_update)

Outputs:

{
    'name': 'Pluutoo',
    'details': {
        'color': 'blue',
        'tail': True
    }
}

Thanks rednaw for edits.

Recursively/deep update a dict

def deepupdate(original, update):
    """
    Recursively update a dict.
    Subdict's won't be overwritten but also updated.
    """
    for key, value in original.iteritems(): 
        if key not in update:
            update[key] = value
        elif isinstance(value, dict):
            deepupdate(value, update[key]) 
    return update
Demonstration:
pluto_original = {
    'name': 'Pluto',
    'details': {
        'tail': True,
        'color': 'orange'
    }
}

pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }

print deepupdate(pluto_original, pluto_update)

Outputs:

{
    'name': 'Pluutoo',
    'details': {
        'color': 'blue',
        'tail': True
    }
}

Thanks rednaw for edits.

rewrote with rednaw's edits
Source Link
Stan
  • 4.2k
  • 2
  • 33
  • 40

Recursively/deep update a dict

def mergedeepupdate(d1original, d2update):
    """
    Recursively update a dict.
    Subdict's won't be overwritten but also updated.
    """
    for k1key,v1 value in d1original.iteritems(): 
        if not k1key in d2update:
            d2[k1]update[key] = v1value
        elif isinstance(v1value, dict):
            mergedeepupdate(v1value, d2[k1]update[key]) 
    return d2update
Demonstration:
pluto_original = {
    'name': 'Pluto',
    'details': {
        'tail': True,
        'color': 'orange'
    }
}

pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }

print deepupdate(pluto_original, pluto_update)

Outputs:

{
    'name': 'Pluutoo',
    'details': {
        'color': 'blue',
        'tail': True
    }
}

Thanks rednaw for edits.

def merge(d1, d2):
    for k1,v1 in d1.iteritems():
        if not k1 in d2:
            d2[k1] = v1
        elif isinstance(v1, dict):
            merge(v1, d2[k1])
    return d2

Recursively/deep update a dict

def deepupdate(original, update):
    """
    Recursively update a dict.
    Subdict's won't be overwritten but also updated.
    """
    for key, value in original.iteritems(): 
        if not key in update:
            update[key] = value
        elif isinstance(value, dict):
            deepupdate(value, update[key]) 
    return update
Demonstration:
pluto_original = {
    'name': 'Pluto',
    'details': {
        'tail': True,
        'color': 'orange'
    }
}

pluto_update = { 'name': 'Pluutoo', 'details': { 'color': 'blue' } }

print deepupdate(pluto_original, pluto_update)

Outputs:

{
    'name': 'Pluutoo',
    'details': {
        'color': 'blue',
        'tail': True
    }
}

Thanks rednaw for edits.

Source Link
Stan
  • 4.2k
  • 2
  • 33
  • 40

def merge(d1, d2):
    for k1,v1 in d1.iteritems():
        if not k1 in d2:
            d2[k1] = v1
        elif isinstance(v1, dict):
            merge(v1, d2[k1])
    return d2