A short recipe that illustrates how to create an n-dimensional dictionary where you can set a new key-value pair at an arbitrary level of nesting with simple code by using the __getitem__() and setdefault() functions. Note: this is best used by assuming your object tree is always N-dimensional and that N is constant for all values, otherwise you may encounter an error. Consider:
>>> class auto_dict(dict):
...     def __getitem__(self, key):
...         return self.setdefault(key, self.__class__())
...
>>> d = auto_dict()
>>> d["foo"]=1
>>> d
{'foo': 1}
>>> d["bah"]["bug"] = 2
>>> d
{'foo': 1, 'bah': {'bug': 2}}
>>> d["bah"]["bug"]["big"] = 3
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: object does not support item assignment
>>> d["xbah"]["xbug"]["xbig"] = 3
>>> d
{'xbah': {'xbug': {'xbig': 3}}, 'foo': 1, 'bah': {'bug': 2}}
>>> d["xbah"]["bug"]["big"] = 3
>>> d
{'xbah': {'xbug': {'xbig': 3}, 'bug': {'big': 3}}, 'foo': 1, 'bah': {'bug': 2}}
>>>
[1] [2]