+ 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.

21st Mar 2018, 2:28 AM
Maninder $ingh
Maninder $ingh - avatar
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!
21st Mar 2018, 3:27 AM
777
777 - avatar
+ 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]]
21st Mar 2018, 8:51 AM
Amaras A
Amaras A - avatar