When writing to a dictionary using a list today , I found that the contents of the list are all the same dictionaries , And it's the last dictionary to write
example :
a=[] b={ "name":" " } for i in range(4): b["name"]=i a.append(b) print(a)
The running results are as follows :
[{'name': 3}, {'name': 3}, {'name': 3}, {'name': 3}]
?  ? ?

It's not. It should be 0,1,2,3 Is that right , Why all of them 3 What about it , I found some information and found some problems , The original list uses append When adding data to a list, you do not write the complete dictionary data to the list , Instead, write the address of the dictionary data , And the above way to modify the memory address of the data , This causes the list to store all the addresses of the same dictionary , The last output from the address is the last dictionary

terms of settlement
have access to copy() Solve this problem
a=[] b={ "name":" " } for i in range(4): b["name"]=i a.append(b.copy()) print(a
)
output :
[{'name': 0}, {'name': 1}, {'name': 2}, {'name': 3}]
Perfect solution !

Technology
Daily Recommendation