efficient updating of nested dictionaries

Sidharthk sidharthk at hotmail.com
Mon Jan 26 05:44:36 EST 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)
        else:
            mydict[key] = newValue
    mydict.update(dict(newKeysPairs))
    return mydict

regards

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
dictionaries?



More information about the Python-list mailing list