Why "Shallow copy" appends on this examples?

a=[[0]*5]*4 pass #breakpoint
a[2][2]=1 pass #breakpoint
` print(a)

when i run this scripts it returned :

` [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]

but i wish it returned out like this:

` [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]

so why? I know how to use ‘for’ command to create an list that could fix this problem.but sorry i am new to python and i just study it to pass a exam. pwp
I don’t want to get so caught up in this simple question that it consumes too much time.awa

so is this a bug? sorry my english is so poor.

After multiplication the references are to the same object:

>>> a = [[0]*5]*4
>>> for item in a:
...     print(id(item))
...
4372787712
4372787712
4372787712
4372787712
1 Like