I need your help. I am confused about the output of the following code.
a = []
b = []
for j in range(2):
b.append(j)
a.append(b)
print(b)
print(a)
for i in range(2):
b.append(i)
print(b)
print(a)
a.append(b)
print(a)
I thought that the 3rd "print(a)" should give [[0,1],[0, 1, 0, 1], but it actually gives [[0, 1, 0, 1], [0, 1, 0, 1]]. It seems that after the 2nd loop the "a" is automatically updated, sth that is not happening in the following case, for example, where both prints should give 0:
x=0
y=x
print(y)
x=9
print(y)
Does anyone know what changes from the 1st "print(a)" to the 2nd and why they give different outputs?
Thanks!