title: Modifying Python lists via slices and for-loops?
body: <p>I was trying to modify the values in lists via slices and for-loops, and ran into some pretty interesting behavior. I would appreciate if someone could explain what's happening internally here.</p>
  
  <pre><code>&gt;&gt;&gt; x = [1,2,3,4,5]
  &gt;&gt;&gt; x[:2] = [6,7] #slices can be modified
  &gt;&gt;&gt; x
  [6, 7, 3, 4, 5]
  &gt;&gt;&gt; x[:2][0] = 8 #indices of slices cannot be modified
  &gt;&gt;&gt; x
  [6, 7, 3, 4, 5]
  &gt;&gt;&gt; x[:2][:1] = [8] #slices of slices cannot be modified
  &gt;&gt;&gt; x
  [6, 7, 3, 4, 5]
  &gt;&gt;&gt; for z in x: #this version of a for-loop cannot modify lists
  ...    z += 1
  ... 
  &gt;&gt;&gt; x
  [6, 7, 3, 4, 5]
  &gt;&gt;&gt; for i in range(len(x)): #this version of a for-loop can modify lists
  ...    x[i] += 1
  ... 
  &gt;&gt;&gt; x
  [7, 8, 4, 5, 6]
  &gt;&gt;&gt; y = x[:2] #if I assign a slice to a var, it can be modified...
  &gt;&gt;&gt; y[0] = 1
  &gt;&gt;&gt; y
  [1, 8]
  &gt;&gt;&gt; x #...but it has no impact on the original list
  [7, 8, 4, 5, 6]
  </code></pre>