[Python-ideas] Nested dictionaries

Philipp A. flying-sheep at web.de
Sat Mar 29 17:43:31 CET 2014


2014-03-29 16:46 GMT+01:00 Chris Angelico <rosuav at gmail.com>:

Alternatively, you may find it simpler and easier to use a non-nested
> dictionary. I store persistent data for my MUD client in a structure
> like this:
>
> persist["window/downarr"] = blah blah
>
> Of course, this assumes your keys are simple strings, and it's best
> for literals; you can't easily iterate over "everything under
> window/", for instance. But if that covers your requirements, it's
> probably going to be easier to read and comprehend; and it'd probably
> be faster, too, although that shouldn't be a primary consideration.
>
> ChrisA
>
another possibility: use tuples as keys.

persist['window', 'downarr'] = blah blah

this works with all hashable types.

PS: to elaborate for non-gurus: tuples in python are created by the comma,
not the braces, so the above is another way to write persist[ ('window',
'downarr') ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140329/39b1ba4e/attachment.html>


More information about the Python-ideas mailing list