1

I have this nested dictionary:

userdict = {'userone': {'valueone': 1, 'valuetwo': 1}}
userdict['usertwo']={'valueone': 1, 'valuetwo': 1}

I process this with:

for i in userdict.keys():
    print(i)
    for j in userdict[i].keys():
        print(j)
    
        userdict[i]={'valuetwo': 0}

After this process i get:

{'userone': {'valuetwo': 0}, 'usertwo': {'valuetwo': 0}}

but i want:

{'userone': {'valueone': 1, 'valuetwo': 0}, 'usertwo': {'valueone': 1, 'valuetwo': 0}}

How can i stop overwriting "valueone"?

1
  • Great question. It's well-posed because it has the minimal code to reproduce the issue, it has the output you get, and the expectation. Nice one! Commented Jan 23, 2022 at 22:23

1 Answer 1

1

You're overwriting the entire dictionary at userdict[i], but instead you want to overwrite the entry for userdict[i]['valuetwo'], so

userdict[i]['valuetwo'] = 0
Sign up to request clarification or add additional context in comments.

Comments

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.