efficient updating of nested dictionaries

Sidharthk sidharthk at hotmail.com
Mon Jan 26 11:44:36 CET 2004

This is untested code but i think it should work.(fingers crossed)
Btw i doubt this will be fast though.

def rec_update(mydict, newdict):
    presentKeysPairs = [(key,value)
                        for (key, value) in newdict.items()
                        if mydict.has_key(key)]
    newKeysPairs = [(key,value)
                    for (key, value) in newdict,items()
                    if not mydict.has_key(key)]
    for key, newValue in presentKeysPairs:
        currentValue = mydict[key]
        if isisntance(newValue, dict):
            mydict[key] = rec_update(newValue)
            mydict[key] = newValue
    return mydict


ps. why can't you simply use tuples to represent the different
dimensions, even if the number of dimensions vary.
is there any particular reason why you are using these nested

More information about the Python-list mailing list