title: In-place modification of Python lists
body: <p>I am trying to perform in-place modification of a list of list on the level of the primary list. However, when I try to modify the iterating variable (<code>row</code> in the example below), it appears to create a new pointer to it rather than modifying it.</p>
  
  <hr>
  
  <p>Smallest example of my problem. </p>
  
  <pre><code>c = [1,2,3]
  for x in c:
      x = x + 3
  print(c) #returns [1,2,3], expected [4,5,6]
  </code></pre>
  
  <p>The above example is a trivial example of my problem. Is there a way to modify <code>x</code> elementwise, in-place and have the changes appear in C?</p>
  
  <hr>
  
  <p>Less trivial example of my problem. I am switching all 0's to 1's and vice-versa.</p>
  
  <pre><code>A =  [[1,1,0],
        [1,0,1],
        [0,0,0]]
  for row in A:
      row = list(map(lambda val: 1 - val, row))
  print(A)
  </code></pre>
  
  <p>Expected </p>
  
  <pre><code>A = [[0,0,1],
       [0,1,0],
       [1,1,1]]
  </code></pre>
  
  <p>Returned</p>
  
  <pre><code>A = [[1,1,0],
       [1,0,1],
       [0,0,0]]
  </code></pre>
  
  <hr>
  
  <p>update:
  Great answers so far. I am interested how the iterating variable (<code>row</code> in the second example) is linked to the iterable variable (<code>A</code> in the second example).</p>
  
  <p>If I do the following, which reverses each sublist of A, it works perfectly.</p>
  
  <p>Why does the following example, where I modify the iterating variable works but the above examples do not?</p>
  
  <pre><code>A =  [[1,1,0],
    [1,0,1],
    [0,0,0]]
  
  for row in A:
      row.reverse()
  print(A)
  
  #returns, as expected
  A = [[0, 1, 1],
       [1, 0, 1], 
       [0, 0, 0]]
  </code></pre>