-2
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?

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
anon
  • 9
  • 3
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Be Chiller Too Nov 15 '21 at 14:48
  • 2
    OK so you must read about shallow copy and deep copy. A new list is made and values are copied in y but referencing to [12] list is not made. Suppose that instead of [12] there is variable holding address of this list [12]. So a new variable is created holding the same address of this list [12]. Due to this changing its value is changing the value of the list in both due to same address referencing but not others as they are deep copy. – Shobhit Tewari Nov 15 '21 at 14:52
  • y = x instead of y = list(x) gives the behavior you want. – ikamen Nov 15 '21 at 14:56
  • can you make it a bit more clearer @ShobhitTewari? its not clear to me – anon Nov 15 '21 at 14:59

1 Answers1

2

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]]

typedecker
  • 1,351
  • 2
  • 13
  • 25