How to update a value in a nested dictionary in python3?
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"?
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