In the following code, I created a simple dictionary, then I want to change the value of key B, but all the other values are changed,If the statement score.setdefault(name, values) is replaced by score.setdefault(name, [0,0,0]),then it works ok。I am so confused could somebody help me? Thank you very much.
names = ['A', 'B', 'C']
score = {}
values = [0,0,0]
#Create a dictionary: {'A': [0, 0, 0], 'B': [0, 0, 0], 'C': [0, 0, 0]}
for name in names:
score.setdefault(name, values)
print(score)
#Change the value of key B
for name in names:
score['B'][0] = 1
print(score)
#The result is: {'A': [1, 0, 0], 'B': [1, 0, 0], 'C': [1, 0, 0]}