Can someone explain this expression: a[len(a):] = [x] equivalent to list.append(x)
<p>I'm at the very beginning of learning Python 3. Getting to know the language basics. There is a method to the list data type:</p>

<pre><code>list.append(x)
</code></pre>

<p>and in the tutorial it is said to be equivalent to this expression:</p>

<pre><code>a[len(a):] = [x]
</code></pre>

<p>Can someone please explain this expression? I can't grasp the <strong>len(a):</strong> part. It's a slice right? From the last item to the last? Can't make sense of it.</p>

<p>I'm aware this is very newbie, sorry. I'm determined to learn Python for Blender scripting and the Game Engine, and want to understand well all the constructs.</p>

<p>Think back to how slices work: <code>a[beginning:end]</code>.
If you do not supply one of them, then you get all the list from <code>beginning</code> or all the way to <code>end</code>.</p>

<p>What that means is if I ask for <code>a[2:]</code>, I will get the list from the index <code>2</code> all the way to the end of the list and <code>len(a)</code> is an index right after the last element of the array... so <code>a[len(a):]</code> is basically an empty array positioned right after the last element of the array.</p>

<p>Say you have <code>a = [0,1,2]</code>, and you do <code>a[3:] = [3,4,5]</code>, what you're telling Python is that right after <code>[0,1,2</code> and right before <code>]</code>, there should be <code>3,4,5</code>.
Thus <code>a</code> will become <code>[0,1,2,3,4,5]</code> and after that step <code>a[3:]</code> will indeed be equal to <code>[3,4,5]</code> just as you declared.</p>

<p>Edit: as chepner commented, any index greater than or equal to <code>len(a)</code> will work just as well. For instance, <code>a = [0,1,2]</code> and <code>a[42:] = [3,4,5]</code> will also result in <code>a</code> becoming <code>[0,1,2,3,4,5]</code>.</p>

python list python-3.x