Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason why append and insert are both there?

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?

like image 759
danicotra Avatar asked Jul 30 '15 19:07

danicotra


People also ask

Is append and insert same?

The only difference between append() and insert() is that insert function allows us to add a specific element at a specified index of the list unlike append() where we can add the element only at end of the list.

Why it is necessary to have both the functions append and extend?

Both these methods are used to manipulate the lists in their specific way. The append method adds a single or a group of items (sequence) as one element at the tail of a list. On the other hand, the extend method appends the input elements to the end as part of the original list.


2 Answers

The difference between append and insert here is the same as in normal usage, and in most text editors. Append adds to the end of the list, while insert adds in front of a specified index. The reason they are different methods is both because they do different things, and because append can be expected to be a quick operation, while insert might take a while depending on the size of the list and where you're inserting, because everything after the insertion point has to be reindexed.

I'm not privy to the actual reasons insert and append were made different methods, but I would make an educated guess that it is to help remind the developer of the inherent performance difference. Rather than one insert method, with an optional parameter, which would normally run in linear time except when the parameter was not specified, in which case it would run in constant time (very odd), a second method which would always be constant time was added. This type of design decision can be seen in other places in Python, such as when methods like list.sort return None, instead of a new list, as a reminder that they are in-place operations, and not creating (and returning) a new list.

like image 147
Two-Bit Alchemist Avatar answered Nov 14 '22 22:11

Two-Bit Alchemist


The other answer is great -- but another fundamental difference is that insert is much slower:

$ python -m timeit -s 'x=list(range(100000))' 'x.append(1)'
10000000 loops, best of 3: 0.19 usec per loop
$ python -m timeit -s 'x=list(range(100000))' 'x.insert(len(x), 1)'
1000000 loops, best of 3: 0.514 usec per loop

It's O(n), meaning the time it takes to insert at len(x) scales linearly with the size of your list. append is O(1), meaning it's always very fast, no matter how big your list is.

like image 44
Lynn Avatar answered Nov 14 '22 22:11

Lynn