0

Hello I wondering how can I do to to this :

a["Hello"]["Test"]["Sun"] = 3

I tried like this but it does not work.

Could you help me please ?

Peter Carles
  • 55
  • 2
  • 6

1 Answers1

0

Perhaps you are coming from a language like PHP. In Python arrays (or lists as they are called in Python) cannot have string indices, but you can use a dictionary for this.

To make assigning items work using that type of syntax a["Hello"]["Test"] must already exist as a dictionary, and that requires a["Hello"] to exist as a dictionary, etc.

a = {}
a["Hello"] = {}
a["Hello"]["Test"] = {}
a["Hello"]["Test"]["Sun"] = 3

But you can do it all at once like this.

a = {'Hello': {'Test': {'Sun': 3}}}

Or a combination of the two.

a = {}
a["Hello"] = {'Test': {'Sun': 3}}
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51