Accessing A Dictionary Item That is an Array

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]])

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:

  • menu['items'] to return the list;

  • followed by indexing the n-th element of the list.

Putting them together with the call to print gives us:

print(menu['items'][0])

which prints “Temp Control”.

1 Like

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.