I recently came across a strange observation while creating list of dictionaries by appending dictionaries to a list.
Below is my code:
a = []
for i in range(5):
m = {'a':'apple'}
a.append(m)
m['b'] = 'ball'
for i in a:
print(i)
I expected that the list a will only contain 'a': 'apple' as the key b is defined after the append statement.
Suprisingly, below is the output I obtained:
{'a': 'apple', 'b': 'ball'}
{'a': 'apple', 'b': 'ball'}
{'a': 'apple', 'b': 'ball'}
{'a': 'apple', 'b': 'ball'}
{'a': 'apple', 'b': 'ball'}
Why does this happen? Thanks in advance!