efficient updating of nested dictionaries

Rich Krauter rmkrauter at yahoo.com
Mon Jan 26 04:37:19 CET 2004


The following is probably too dependent on the data type of the keys,
but it may be suitable in some programs. It's certainly not a general
solution for all cases. Others will have much better ideas, but here
goes anyway ...

You may want to use a non-nested dict with a 'superkey' composed of the
concatenation of the three keys, seperated by some delimiter.
use MY_DICT[KEY_X+'_'+KEY_Y+'_'+KEY_Z]=FOO

Then you could use update().You would just have to do some pre- and
post-processing of the keys. i.e. splitting or joining the 'superkey' by
the delimiter you choose.

Although, that's probably kind of lame - I bet others will have much
better suggestions. I'm interested in how other people do this too.
Rich


On Sun, 2004-01-25 at 21:33, 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?
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040125/60ffed61/attachment.html>


More information about the Python-list mailing list