men6288
men6288

Reputation: 69

Setting values in a python dictionary

When it comes to declaring new values in a python dictionary how come this works:

app_dict = {}
app_dict['properties'] = { "name": "test" }

But something like this doesn't: (keyerror)

app_dict = {}
app_dict['properties']['version'] = {"name":"dino"}

Upvotes: 0

Views: 60

Answers (1)

Ibukun Muyide
Ibukun Muyide

Reputation: 1298

for the first part

app_dict = {}
app_dict['properties'] = { "name": "test" }

if a key doesn't exist, it will be created, you initially defined app_dict = {} as a dictionary object, hence app_dict['properties'] = { "name": "test" } is valid because it created a properties key and assigned the content as another dictionary {'name': 'test'}

for the second part

app_dict = {}
app_dict['properties']['version'] = {"name":"dino"}

the app_dict['properties'] doesn't exist yet so it's not a dictionary , hence assigning app_dict['properties']['version'] = {"name":"dino"} will thrown an error KeyError: 'properties'.

if you have done it this way

app_dict = {}
app_dict['properties'] = { "name": "test" }
app_dict['properties']['version'] = {"name":"dino"}

This would have worked , because app_dict['properties'] is already created as it's declared as a dictionary , hence app_dict['properties']['version'] = {"name":"dino"} will only create a key in the dictionary assigning it to another dictionary

Upvotes: 2

Related Questions