title: Python Box and Pointers in lists
body: <p>I'm studying for my final semester written paper. I was taught to trace the mutations of lists using box and pointer diagrams. However there was one question I came across where my method didn't work.</p>
  
  <pre><code>#Main code 1
  a = [1, 2]
  b = [a, a]
  c = a.copy()
  c[0], a[1] = b[1], c[0] #replace this
  
  #code A
  ##c[0]=b[1]
  ##a[1]=c[0]
  
  #code B
  ##a[1]=c[0]
  ##c[0]=b[1]
  
  print(a)
  print(b)
  print(c)
  
  ##Normal Output/Code B
  ##[1, 1]
  ##[[1, 1], [1, 1]]
  ##[[1, 1], 2]
  
  ##Code A output
  ##[1, [...]]
  ##[[1, [...]], [1, [...]]]
  ##[[1, [...]], 2]
  </code></pre>
  
  <p>The main code is written as it is, and the given answer is shown in <code>Normal Output</code>(proven by Python3 IDLE). When I did the tracing on paper, I thought that the code would produce <code>Code A output</code> instead. </p>
  
  <blockquote>
    <p>Is there any knowledge that I am missing out here?</p>
  </blockquote>
  
  <p>Here are some other similar mutation questions that my method of tracing has worked in, but I can't draw any differences to why this behaviour is only seen in the first code.</p>
  
  <pre><code>#Code2 where switching line 3 like code 1 doesn't matter
  a = [['a', 'b'], ['c'], 'd']
  b = a[:-1]
  a[1], b[0][1] = b[0], a[2] #switch this
  print(a)
  print(b)
  
  ##[['a', 'd'], ['a', 'd'], 'd']
  ##[['a', 'd'], ['c']]
  
  #Code3 that has the same output even with a 1 liner replacement
  a = [1,2,3]
  b = [a,3,4]
  a[2] = b
  b[0][0] = b
  a[0][1] = 99
  a[2][0] = 5
  #a[2],b[0][0],a[0][1],a[2][0]=b,b,99,5 #1 liner replacement
  print(a)
  
  #[[5, 99, 4], 2, [5, 99, 4]]
  </code></pre>