I'm surely not the Python guru I'd like to be and I mostly learn studying/experimenting in my spare time, it is very likely I'm going to make a trivial question for experienced users... yet, I really want to understand and this is a place that helps me a lot.
Now, after the due premise, Python documentation says:
4.6.3. Mutable Sequence Types
s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x])
[...]
s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x])
and, moreover:
5.1. More on Lists
list.append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x].
[...]
list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
So now I'm wondering why there are two methods to do, basically, the same thing? Wouldn't it been possible (and simpler) to have just one append/insert(x, i=len(this))
where the i
would have been an optional parameter and, when not present, would have meant add to the end of the list?