x=[1,[2]]
y=list(x)
x[0]=-1
x[1][0] = -1
print(y)
I thought the answer would be [1,[2]] as the changes were made after declaring variable y but the output i get is [1,[-1]]. Please explain the reason.
x=[1,[2]]
y=list(x)
x[0]=-1
x[1][0] = -1
print(y)
I thought the answer would be [1,[2]] as the changes were made after declaring variable y but the output i get is [1,[-1]]. Please explain the reason.
When you assing y you are not making a new list, you are pointing to the same object as x. So when you update x, y is now pointing to the "new" version of x
edit-- @S3DEV pointed out that this answer is is not that precise, list(x) has made a copy (new object) of x; albeit, a shallow copy. So it has made a copy of x, but not the second element of the list witch is an other list. y[1] is pointing to the same object that x[1] is pointing to.