Mutability of lists in python
<p>Example one: Changing the value that has been appended to b changes the value in the original list l</p>

<pre><code>&gt;&gt;&gt; l = [1 , 2, 3]
&gt;&gt;&gt; b = []
&gt;&gt;&gt; b.append(l)
&gt;&gt;&gt; b[0].append(4)
&gt;&gt;&gt; b
[[1, 2, 3, 4]]
&gt;&gt;&gt; l
[1, 2, 3, 4]
</code></pre>

<p>Example 2: l1 is appended to ans and then the value of l1 is modified. However, the value in ans remains the same.</p>

<pre><code>&gt;&gt;&gt; l1 = [1, 2, 3]
&gt;&gt;&gt; ans = []
&gt;&gt;&gt; ans.append(l1)
&gt;&gt;&gt; ans
[[1, 2, 3]]
&gt;&gt;&gt; l1 = [2, 3, 4]
&gt;&gt;&gt; ans
[[1, 2, 3]]
</code></pre>

<p>I seem to be missing some fundamental point about the mutability of lists. Could someone please point it out?</p>

<p>You are not mutating <code>l1</code>. You are assigning a new object to the same name, which makes that name point to a new object. You are moving a label, not modifying an object. The only remaining reference to the list that <code>l1</code> used to point to is now <code>ans[0]</code>.</p>

python list mutable