Consider this simple Python dictionary and I want to change all the values to 4:
d = {'a': 1, 'b': 2, 'c': 3}
I know the following works the intended way:
for key, value in d.items():
d[key] = 4
print(d) # output: {'a': 4, 'b': 4, 'c': 4}
However, this doesn't:
for key, value in d.items():
value = 4
print(d) # output: {'a': 1, 'b': 2, 'c': 3}
When I inspected outputs of built-in id
function of d[key]
and value
, they were the same.
Could anyone explain why value = 4
didn't work?