Question: I have a large list like:

```
[A][B1][C1]=1
[A][B1][C2]=2
[A][B2]=3
[D][E][F][G]=4

```

I want to build a multi-level dict like:

```
A
--B1
-----C1=1
-----C2=1
--B2=3
D
--E
----F
------G=4

```

I know that if I use recursive defaultdict I can write `table[A][B1][C1]=1`, `table[A][B2]=2`, but this works only if I hardcode those insert statement. 

While parsing the list, I don't how many []'s I need beforehand to call `table[key1][key2][...]`.

Answer: 
I'd do it with a subclass of `dict` that defines `__missing__`:

```
>>> class NestedDict(dict):
...     def __missing__(self, key):
...             self[key] = NestedDict()
...             return self[key]
...
>>> table = NestedDict()
>>> table['A']['B1']['C1'] = 1
>>> table
{'A': {'B1': {'C1': 1}}}

```

You can't do it directly with defaultdict because [defaultdict expects the factory function](http://docs.python.org/library/collections.html#collections.defaultdict) at initialization time, but at initialization time, there's no way to describe the same defaultdict. The above construct does the same thing that default dict does, but since it's a named class (NestedDict), it can reference itself as missing keys are encountered. It is also possible to subclass defaultdict and override `__init__`.
To add to @Hugo   
To have a max depth:

```
l=lambda x:defaultdict(lambda:l(x-1)) if x>0 else defaultdict(dict)
arr = l(2)

```