In python, I want to make a 2D array with dictionaries. I do have knowledge of references, so I explicitly used .copy
. When I print the array out, however, the dictionaries that I do not want to be changed also changes.
My code is below.
dicts = []
for j in range(3):
dicts.append([{0:0,1:0,2:0,3:0}.copy()].copy() * 3)
dicts[0][0][0] = 5
dicts[1][1][0] = 10
print(dicts)
OUTPUT:
[[{0: 5, 1: 0}, {0: 5, 1: 0}], [{0: 0, 1: 10}, {0: 0, 1: 10}]]
Does anyone know why this happens, and anyway to fix it? Thank you.