Multiple dict in python

How can I build a nested dictionary from reading configuration files in Python so that I can access it like this:

multi_dict["name1"]["name2"]["name3"] = some_value

      

My current approach:

if name1 not in dict:
    dict["name1"] = {}
    dict["name1"]["name2"] = {}

      

(etc.).

But this path seems so boring! Is there a more elegant way to do this?

The config files I am reading are as follows:

[a1]
key1=value1
[a2]
[.a3]
key2=value2
[..a4]
key3=value3

      

The prefixes .

mean "one more nesting level". So in this example I would set:

dict[a1][key1]=value1
dict[a2][a3][key2]=value2
dict[a2][a3][a4][key3]=value3

      

+3


source to share





All Articles