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: 
This is equivalent to the above, but avoiding lambda notation. Perhaps easier to read ?

```
def dict_factory():
   return defaultdict(dict_factory)

your_dict = dict_factory()

```

Also -- from the comments -- if you'd like to update from an existing dict, you can simply call

```
your_dict[0][1][2].update({"some_key":"some_value"})

```

In order to add values to the dict.
Dan O'Huiginn posted a very nice solution on his journal in 2010:

<http://ohuiginn.net/mt/2010/07/nested_dictionaries_in_python.html>

```
>>> class NestedDict(dict):
...     def __getitem__(self, key):
...         if key in self: return self.get(key)
...         return self.setdefault(key, NestedDict())

>>> eggs = NestedDict()
>>> eggs[1][2][3][4][5]
{}
>>> eggs
{1: {2: {3: {4: {5: {}}}}}}

```