1

I am trying to update a single element in my nested list structure

I wanted to initialize an empty list as below

a= [[0]*2]*2
a
[[0, 0], [0, 0]]

Now i want to update only the 0,0 th element. So i end up assigning that like below.

a[0][0] = 999

However i notice that the 0th element of each nested list is getting updated.

a
[[999, 0], [999, 0]]

So i have noticed that this does not happen if i Initialize the list as below

b = [[0,0],[0,0]]

b[0][0]=999

b
[[999, 0], [0, 0]]

Can anyone help in my understanding of the above two different approaches and why one works and the other does not.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

0 Answers0