The question is simple. Try to pridict output of code lines below:
arr = [[0]*5]*5
arr[0][0] = 1
for row in arr:
print(row)
I expected the result should be as:
[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]
But to my surprise, the 1st element of all rows are changed to 1, not only arr[0][0]. actually my Python prints as:
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
Is there anyone explain this to me?