0

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"}

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this seemed to answer the issue I was having!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.