I initialized a 2d array and set all the elements to False
. Then in my code I want to switch certain elements to True
if conditions are met. However, arr[i][j] = True
sets ALL j elements in arr
to True
. Why is that, and what is the correct syntax?
arr = [[False]*3]*4
print(arr)
arr[1][2] = True
print(arr)
This gives
[[False, False, False], [False, False, False], [False, False, False], [False, False, False]]
which is what we expected, and then
[[False, False, True], [False, False, True], [False, False, True], [False, False, True]]
where all the arr[i][2]'s are set to True, instead of just arr[1][2]