Skip to content Skip to sidebar Skip to footer

Create A Dictionary With Name Of Variable

I am trying to write a program to parse a file, break it into sections, and read it into a nested dictionary. I want the output to be something like this: output = {'section1':{'ne

Solution 1:

You can name a dictionary entry from a variable. If you have

text = "myKey" # or myNumber or any hashable type
data = dict()

You can do

data[text] = anyValue

Solution 2:

Store all your dictionaries in a single root dictionary.

all_dicts['output'] = {'section1':{'nested_section1':{'value1':'value2'}}}

As you merge dictionaries, remove the children from all_dicts.

all_dicts['someotherdict']['key'] = all_dicts['output']
del all_dicts['output']

Post a Comment for "Create A Dictionary With Name Of Variable"