x=[3,6,9,[12]]
y=list(x)
x[3][0]=50
x[1]=25
print(y)
The output of the code is [3, 6, 9, [50]]
Why did 12 change to 50, but 6 didn't change to 25?
x=[3,6,9,[12]]
y=list(x)
x[3][0]=50
x[1]=25
print(y)
The output of the code is [3, 6, 9, [50]]
Why did 12 change to 50, but 6 didn't change to 25?
When you do list(x)
, python makes a copy of the list x and stores it in y. So if it was a list with all elements as integers for example -:
x = [1, 2, 3, 4]
y = list(x)
x[0] = 7
print(x, y)
[7, 2, 3, 4] [1, 2, 3, 4]
You will notice no change in the 0th index of y, that is because the integers are stored and copied with the list.
Python generally stores values at different memory addresses and variables are like pointers that point to that address so the value can be fetched as and when needed.[Thanks for pointing this out in the comments of the question @ShobitTewari]
So a list for example is a reference to the list stored in memory [1, 2, 3, 4]
.
But if the list has a nested list in it like so -: [1, 2, [12]]
then this inner list [12]
is also having its value at some other memory address and the index just points to that address. Unlike integers nested lists aren't stored at the same address as the rest of the list.
So when you do list(x)
, python copies the whole list into a new memory address more on that here. But if you have a nested list, then the memory address of that lists still stays the same.
So when you change the value of that nested list's 0th index, it changes it in the memory and the pointer of y still points to that old memory address, so value of the nested list changes.
x = [1, 2, 3, [4]]
y = list(x)
x[3][0] = 7
print(x, y)
[1, 2, 3, [7]] [1, 2, 3, [7]]