I just thought that I would write it down when I saw it - it would be nice to get confirmation of this behavior; I saw How to pass a variable by reference? but I'm not sure how to interpret it in this context.
Let's say we have these two arrays / lists:
a = [1, 2, 3, 4] b = [-1, a, -100, a[2], -1]
At first, the interpreter sees them as:
>>> print(a) [1, 2, 3, 4] >>> print(b) [-1, [1, 2, 3, 4], -100, 3, -1]
Now change a[2] and see what happens:
>>> print(a) [1, 2, 55, 4] >>> print(b) [-1, [1, 2, 55, 4], -100, 3, -1]
So, wherever list b has a link to list a , the value has been updated, but wherever b was an initialized (link to?) Element from list a , it seems like Python expanded the value during initialization and thus saved the element by value (not by reference), so its value is not explicitly updated.
Basically, I found a use case where it would be convenient to define, for example. b = [-1 a[2] -1] , and then update a[2] and take into account that the last value of a[2] will be issued when the value (in this case) b[1] received. Is there a way to do this in Python, without having to do b = [-1 a -1] and then read b[1][2] (I would like to get the value of a[2] only with b[1] )?