+ 3
Why x!=y?
x=[[0,0],[0,0]] x[0][0]=5 y=[[0]*2]*2 y[0][0]=5 print(x==y) #but output = False why can anyone tell me.
2 Answers
+ 4
This is because of the fact that list y is not evaluated until the compilation of print statement.
Therefore, y[0][0] = 5
changes y as:
y = [[5, 0]] * 2
# which makes the difference ;)
P.S: you can try printing x and y seperately to see the fact in action!
+ 1
@Blue: YES, the list is evaluated before the print function call. And it is not a statement either.
And finally, we get y = [[5,0],[5,0]] and not y = [[5,5],[5,5]]