While answering another question I found the following behaviour:
N=4
a,b = [[0]*N]*2
# [[0]*N]*2: [[0,0,0,0],[0,0,0,0]]
# a: [0,0,0,0]
# b: [0,0,0,0]
for i in range(N):
a[i]= 1
b[i] = 2
# a: [2,2,2,2]
# b: [2,2,2,2]
Leaving aside that this code is not very Pythonic. Can someone explain why a and b are overwriting each other? I would expect that the destructing brakes the original list into two independent lists however (id(a)==id(b))==True
. Is there only truly one list in memory? Why?