test = [{}]*5 # Creates [{}, {}, {}, {}, {}]
print(test[1]) # outputs "{}"
test[1]['asdf'] = 5
print(test)
This gives test
as [{'asdf': 5}, {'asdf': 5}, {'asdf': 5}, {'asdf': 5}, {'asdf': 5}]
. Somehow, all the dictionaries in the list have been set to the same value
But this doesn't happen if we initialize the list a different way:
test2 = [{} for i in range(5)] # Also [{}, {}, {}, {}, {}]
test2[1]['asdf'] = 1
print(test2)
test2
now equals [{}, {'asdf': 1}, {}, {}, {}]
, the expected result.