I created a 2 dimensional list in python using
c=[[0]*5]*3
and tried to update the value at
c[1][4]=c[0][3]+1
but the value get updated at every 4th index of each row. I am not understanding why it happened?
>>> c=[[0]*5]*3
>>> c
[[0, 0, 0, 0, 0] , [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> c[1][4]=c[0][3]+1
>>> c
[[0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]
while doing it using the for loop give the expected output
>>> a=[[0 for i in range(5+1)]for i in range(3+1)]
>>> a
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> a[1][4]=a[0][3]+1
>>>a
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]