Python List assignment and memory allocation
<p>Why does index assignment of another list form a linked list and list slice(one element slice) assignment of another list makes the list part of the original list when both the slice and element with the index points the same element? What changes happen in the memory allocation?</p>

<pre><code>l = [1,2,3,4,5,6]
l[2:3] = [7,8]  # gives [1,2,7,8,5,6]

# whereas:

l[2] = [7,8]  # gives [1,2,[7,8],4,5,6]
</code></pre>

<p>and initialy <code>l[2]=3</code> and <code>l[2:3]=3</code></p>

<p>Your assumption is incorrect, when you say <em>"initialy l[2]=3 and l[2:3]=3"</em>.</p>

<p>This is how it is, in fact:</p>

<pre><code>l = [1, 2, 3, 4, 5, 6]

l[2]  # 3

l[2:3]  # [3]
</code></pre>

<p><code>l[2]</code> is the element at index 2.
<code>l[2:3]</code> is the slice (a sublist) of length one starting at index 2.</p>

<p>Therefore, assigning to <code>l[2]</code> change to element and assigning to <code>l[2:3]</code> replaces a slice with another slice, exactly as you noticed:</p>

<pre><code>l = [1, 2, 3, 4, 5, 6]
l[2:3] = [7,8]  # l is now [1, 2, 7, 8, 4, 5, 6]

l = [1, 2, 3, 4, 5, 6]
l[2] = [7,8]  # l is now [1, 2, [7, 8], 4, 5, 6]
</code></pre>

python list