I was trying to initialize a 2D list in python by arr = [0 * size] * size
.
When I assigned value 1 to the position arr[0][0], arr[1][1],arr[2][2]....., somehow all the positions were assigned to 1. I am confused which part I did wrong. Thank you
n = 3
dp = [[0] * n] * n
print (dp)
for i in range(n):
dp[i][i] = 1
print(dp)
#result from pycharm
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]