Sergio Manchado
Sergio Manchado

Reputation: 143

How to create this variable?

I would like to create a variable similar to a dictionary like that:

parameters['Cell']['volume'] = 5    
parameters['Project']['winterTemp'] = 30

I have tried to do:

parameters = dict()    
parameters['volume'] = 5

It works, but how can I create a "two dimensional" dictionary like the example where I could store the cell number?

Upvotes: 2

Views: 56

Answers (2)

Forge
Forge

Reputation: 6844

The same way you defined parameters to be a dictionary, you can define any key in parameters to be a dictionary too, like this:

parameters = dict()
parameters['Cell'] = dict()   # same as parameters['Cell'] = {}

Or like this:

parameters = {'Cell': {}}

You can also use the collections package to define the default value in parameters to be a dictionary like this:

parameters = collections.defaultdict(dict)
print parameters['Cell']
>> {}

Upvotes: 2

Navid
Navid

Reputation: 120

you need to make a dictionary as value for 'Cell' then assign a key, value of 'volume':5 to it. the answer would be like this:

parameters = {'Cell' : {'volume' : 5}}

Upvotes: 1

Related Questions