Very simple python, I just create a new list object by copying the old list. By using .copy() method, I thought it should create a new object in Python based on the official doc: https://docs.python.org/3.6/library/stdtypes.html?highlight=list#list
How come after I update the elements in the new object, the elements in the old list object also got changed.
old = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
print(old)
new = old.copy()
for i in range(len(new)):
for j in range(len(new[0])):
new[i][j] = 0
print(old)
print(new)
How come the output is, where I expect the old value shouldn't be changed:
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]