[Python-ideas] Nested dictionaries
Terry Reedy
tjreedy at udel.edu
Sat Mar 29 23:20:16 CET 2014
On 3/29/2014 11:36 AM, Richard Prosser wrote:
> I know that there are good solutions like the one at
> http://ohuiginn.net/mt/2010/07/nested_dictionaries_in_python.html
You can file a request on the tracker asking that this simple 4-line
example be added to the doc.
>>> class NestedDict(dict):
... def __getitem__(self, key):
... if key in self: return self.get(key)
... return self.setdefault(key, NestedDict())
> or by using defaultdict but I feel that this really
> should be part of the language.
It is. You just have to copy and paste ;-).
Actually, I am being serious. The example is an application of
dict.setdefault, which *is* in the language.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list