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:
list.append(x)
and in the tutorial it is said to be equivalent to this expression:
a[len(a):] = [x]
Can someone please explain this expression? I can't grasp the len(a): part. It's a slice right? From the last item to the last? Can't make sense of it.
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.
append() adds a single element to the end of the list while . extend() can add multiple individual elements to the end of the list.
When append() method adds its argument as a single element to the end of a list, the length of the list itself will increase by one. Whereas extend() method iterates over its argument adding each element to the list, extending the list.
Definition and Usage The append() method appends an element to the end of the list.
Think back to how slices work: a[beginning:end]
.
If you do not supply one of them, then you get all the list from beginning
or all the way to end
.
What that means is if I ask for a[2:]
, I will get the list from the index 2
all the way to the end of the list and len(a)
is an index right after the last element of the array... so a[len(a):]
is basically an empty array positioned right after the last element of the array.
Say you have a = [0,1,2]
, and you do a[3:] = [3,4,5]
, what you're telling Python is that right after [0,1,2
and right before ]
, there should be 3,4,5
.
Thus a
will become [0,1,2,3,4,5]
and after that step a[3:]
will indeed be equal to [3,4,5]
just as you declared.
Edit: as chepner commented, any index greater than or equal to len(a)
will work just as well. For instance, a = [0,1,2]
and a[42:] = [3,4,5]
will also result in a
becoming [0,1,2,3,4,5]
.
One could generally state that l[len(l):] = [1]
is similar to append
, and that is what is stated in the docs, but, that is a special case that holds true only when the right hand side has a single element.
In the more general case it is safer to state that it is equivalent to extend
for the following reasons:
Append takes an object and appends that to the end; with slice assignment you extend a list with the given iterable on the right hand side:
l[len(l):] = [1, 2, 3]
is equivalent to:
l.extend([1, 2, 3])
The same argument to append
would cause [1, 2, 3]
to be appended as an object at the end of l
. In this scenario len(l)
is simply used in order for the extending of the list to be performed at the end of l
.
Some examples to illustrate their difference:
l = [1, 2]
l[len(l):] = [1, 2] # l becomes [1, 2, 1, 2]
l.extend([1, 2]) # l becomes [1, 2, 1, 2, 1, 2]
l.append([1, 2]) # l becomes [1, 2, 1, 2, 1, 2, [1, 2]]
As you note, l.append(<iterable>)
doesn't actually append each value in the iterable, it appends the iterable itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With