Benjamin Levy
Benjamin Levy

Reputation: 343

Python 3 nested ordered dictionary key access

I have a nested, ordered dictionary built in Python 3. It looks like this:

coefFieldDict = OrderedDict([(('AC_Type',), 
    OrderedDict([('BADA_code', 0), ('n_engine', 1), ('eng_type', 2), 'wake_cate', 3)])), 
   (('Mass',), OrderedDic([('m_ref', 4), ('m_min', 5), ('m_max', 6), ('m_pyld', 7), ('G_w', 8), ('unused', 9)]))
   (('Flight_Env',), OrderedDict([('V_MO', 10), ('M_MO', 11), ('h_MO', 12), ('h_max', 13), ('G_t', 14), ('unused', 15)]))], ...)

Now, I want the list of keys at the top level, which I obtain with:

outerKeys = list(coefFieldDict.keys())

which give me:

[('AC_Type',),
('Mass',),
('Flight_Env',),
('Aero',),
('Thrust',),
('Fuel',),
('Ground',)]

and for an example of one of the keys, I have:

list(coefFieldDict.keys())[1][0]

Out[104]: 'Mass'

Now, on using this valid key into the orderedDict ('coefFieldDict'), I receive this error:

Traceback (most recent call last):

File "<ipython-input-107-61bab1ad7886>", line 1, in <module>

coefFieldDict['Mass']

KeyError: 'Mass'

What I am doing wrong?

Upvotes: 0

Views: 402

Answers (1)

hl037_
hl037_

Reputation: 3897

try coefFieldDict[('Mass',)]

...Since you are using tuples (why ?) instead of strings as key

Upvotes: 1

Related Questions