efficient updating of nested dictionaries

omission9 omission9 at invalid.email.info
Mon Jan 26 00:03:39 EST 2004


omission9 wrote:

> I have a dictionary that looks like this
> MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
> 
> I am having a problem updating this with a simple 
> MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting 
> into the inner dicts.
> Getting the keys of each and iterating through and updating each one is 
> terribly slow as the number of keys gets bigger and bigger.
> What is the bst way to update my nested dicts?
> 
> 
> 
So far I have found this on the internet:
     def rUpdate(self,targetDict,itemDict):
     	valtab=[]		
     	for key,val in itemDict.items():
     	    if type(val)==type({}):
     	        newTarget=targetDict.setdefault(key,{})
     	        self.rUpdate(newTarget,val)
     	    else:
		targetDict[key]=val

However, this does not seem to handle the fact that each dict has 
multiple keys. :( So far the modification I have made to make it work 
right have failed. Any ideas?




More information about the Python-list mailing list