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: 
You can do it without even defining a class:

```
from collections import defaultdict

nested_dict = lambda: defaultdict(nested_dict)
nest = nested_dict()

nest[0][1][2][3][4][5] = 6

```
I think the simplest implementation of a recursive dictionary is this. Only leaf nodes can contain values.

```
# Define recursive dictionary
from collections import defaultdict
tree = lambda: defaultdict(tree)

```

Usage:

```
# Create instance
mydict = tree()

mydict['a'] = 1
mydict['b']['a'] = 2
mydict['c']
mydict['d']['a']['b'] = 0

# Print
import prettyprint
prettyprint.pp(mydict)

```

Output:

```
{
  "a": 1, 
  "b": {
    "a": 1
  }, 
  "c": {},
  "d": {
    "a": {
      "b": 0
    }
  }
}

```