I have the following dictionary defined:
menu={
‘main’:‘Main’,‘index’:0,
‘items’:[‘Temp Control’,‘Blowers’,‘System Info’,‘Network’]
}
I cannot figure out the correct syntax for accessing the nth element of ‘items’.
print(menu[‘main’][‘items’[0]])
1 Like
Gw1500se1 defined a dict:
menu={
'main':'Main',
'index':0,
'items':['Temp Control','Blowers','System Info','Network']
}
then tried this:
print(menu['main']['items'[0]])
Break it down step-by step:
-
menu['main']
returns the string ‘Main’;
-
'items'[0]
returns the character 'i'
;
-
so the third step becomes 'Main'['i']
which fails.
Instead, you need:
Putting them together with the call to print
gives us:
print(menu['items'][0])
which prints “Temp Control”.
2 Likes
Thanks for the reply. I see now. I was thinking one level too deep. I was treating items as if it were an element of main rather than menu.