title: Storing elements of one list, in another list - by reference - in Python?
body: <p>I just thought I'd jot this down now that I've seen it - it would be nice to get a confirmation on this behavior; I did see <a href="https://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference">How do I pass a variable by reference?</a>, but I'm not sure how to interpret it in this context. </p>
  
  <p>Let's say we have these two arrays/lists:</p>
  
  <pre><code>a = [1, 2, 3, 4]
  b = [-1, a, -100, a[2], -1]
  </code></pre>
  
  <p>The interpreter initially sees them as:</p>
  
  <pre><code>&gt;&gt;&gt; print(a)
  [1, 2, 3, 4]
  &gt;&gt;&gt; print(b)
  [-1, [1, 2, 3, 4], -100, 3, -1]
  </code></pre>
  
  <p>Now let's change <code>a[2]</code>, and see what happens:</p>
  
  <pre><code>&gt;&gt;&gt; print(a)
  [1, 2, 55, 4]
  &gt;&gt;&gt; print(b)
  [-1, [1, 2, 55, 4], -100, 3, -1]
  </code></pre>
  
  <p>So, wherever list <code>b</code> has a reference to the <em>list</em> <code>a</code>, the value has been updated - but wherever <code>b</code> was initialized with (a reference to?) an <em>element from</em> list <code>a</code>, it seems that Python expanded the value at initialization time, and thus stored the element by value (not by reference), so it's value obviously doesn't update. </p>
  
  <p>Basically, I found a use case, where it would be convenient to be able to define e.g. <code>b = [-1 a[2] -1]</code>, and then update <code>a[2]</code>, and be able to count that the latest value of <code>a[2]</code> will be emitted when getting the value of (in this case) <code>b[1]</code>. Is there a way to do that in Python, without having to do <code>b = [-1 a -1]</code>, and then reading <code>b[1][2]</code> (I'd like to get the value of <code>a[2]</code> just by using <code>b[1]</code>)? </p>