This is all about mutable objects: python is a bit lazy when it can be, it saves memory by having things of the same value stored in the same place.
Eg >>> a = 5 >>> b = a >>> id(a) == id (b) True
(id () is a function to tell you in essence where it is in the RAM, a and b with the same id means the same chunk of memory stores them)
(Alternatively)
>>> a is b True
This link between a and b stays until you assign one of them to something else, and python realises it has to stop being lazy at some point, and give them their own bits of RAM.
This connection is created when you initialise the list by duplicating the previous rows, the *4 means that the extra lists you now have share the same point(s) in RAM which means they are the same object. Therefore affecting the first item of the top row affects in the same way the first items in all the duplicated rows. This doesn't happen with the one on the right because it was all initialised separately, they all have their own bits of RAM.
2
u/WhenRedditFlies Aug 27 '20 edited Aug 27 '20
Ah! I think I can sort of answer this one.
This is all about mutable objects: python is a bit lazy when it can be, it saves memory by having things of the same value stored in the same place.
Eg
>>> a = 5>>> b = a>>> id(a) == id (b) True(id () is a function to tell you in essence where it is in the RAM, a and b with the same id means the same chunk of memory stores them)
(Alternatively)
>>> a is bTrueThis link between a and b stays until you assign one of them to something else, and python realises it has to stop being lazy at some point, and give them their own bits of RAM.
This shared RAM also applies to lists.
>>> lst1 = [1, 2]>>> lst2 = lst1>>> lst1 is lst2True>>> lst1.append(3)>>> lst1[1, 2, 3]>>> lst2 [1, 2, 3]This connection is created when you initialise the list by duplicating the previous rows, the
*4means that the extra lists you now have share the same point(s) in RAM which means they are the same object. Therefore affecting the first item of the top row affects in the same way the first items in all the duplicated rows. This doesn't happen with the one on the right because it was all initialised separately, they all have their own bits of RAM.