The collections.defaultdict is very useful for well tested code as it cleans up a lot of try-except blocks when dealing with dictionaries. Singly nested dictionary can be done by
. But what about multi-layer nested dictionary?
Essentially I want to do this:
without having to initialise
A recursive function would do. And this is pickle-able too.
Update (Dec 8, 2014): I just learned that this is called autovivification. Very interesting. The Python example there is basically the same as the one presented here.