I wrote a code to catch values in a dictionary as another dictionary value but when I run the code second values over write on pervious value.
I wrote this code:
dict1={}
dict2={}
for i in ['A','B']:
for j in ['age','tall','weight']:
dict2[j]=input()
dict1[i]=dict2
print(dict1)
and entered this inputs:
1
2
3
4
5
6
I expect it shows:
{‘A’: {‘age’: ‘1’, ‘tall’: ‘2’, ‘weight’: ‘3’},’B’: {‘age’: ‘4’, ‘tall’: ‘5’, ‘weight’: ‘6’}}
but it shows me:
{‘A’: {‘age’: ‘4’, ‘tall’: ‘5’, ‘weight’: ‘6’}, ‘B’: {‘age’: ‘4’, ‘tall’: ‘5’, ‘weight’: ‘6’}}
why dict1[‘A’] values over write by dict2
mohammad reza Zardoshti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.