Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List insert at index that is well out of range - behaves like append

Tags:

I had a list

 a = [1, 2, 3] 

when I did

a.insert(100, 100)  [1, 2, 3, 100] 

as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .

Should it not throw

IndexError: list assignment index out of range

exception as it throws when I attempt doing

a[100] = 100 

Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?

Personal Opinion :

Lets see how other languages behave in such a situation :

Ruby :

    > a = [1, 2]     > a[100] = 100     > a  => [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]  

The way ruby handles this is pretty clear and sounds meaningful at least to me .

Java :

In java the method .add(index, value) if applied with index that is out of range(on arraylist , linkedlist for example) will throw java.lang.IndexOutOfBoundsException .

So what I felt was either it should throw exception(as java does) or insert null in the range in between (as ruby handles it ). But the silent way of handling in python is just flummoxing .

UPDATE (16 Sep 2014 IST 8:30 AM) :

As suggested by one of the answerers I posted this question in python-dev and I got a response . It can be seen in this python dev mailing list thread . If you find that the thread link has changed, you can find the answer by doing a google search for the title of question appended at the beginning with python dev.

like image 236
Harish Kayarohanam Avatar asked Sep 15 '14 02:09

Harish Kayarohanam


People also ask

Which is faster insert or append?

If we want to add an element at the end of a list, we should use append . It is faster and direct. If we want to add an element somewhere within a list, we should use insert .

Is insert slower than append?

Insert is slower when compared to append.

How do you add an item to a list at a specific index?

Insert an item into a list: insert() You can insert an item at any index (position) with the insert() method. Set the index for the first parameter and the item to be inserted for the second parameter.


1 Answers

From the docs:

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 technically when you're doing a.insert(100, 100) it ensures that 100 will be inserted at a index before 100 which happens to be, in this case, index 3.

Further, we can have a look at the implementation:

static int ins1(PyListObject *self, Py_ssize_t where, PyObject *v) {     Py_ssize_t i, n = Py_SIZE(self);     PyObject **items;     if (v == NULL) {         PyErr_BadInternalCall();         return -1;     }     if (n == PY_SSIZE_T_MAX) {         PyErr_SetString(PyExc_OverflowError,             "cannot add more objects to list");         return -1;     }      if (list_resize(self, n+1) == -1)         return -1;      if (where < 0) {         where += n;         if (where < 0)             where = 0;     }     if (where > n)  // <-- Here the implementation handles indexes > list-len         where = n;     items = self->ob_item;     for (i = n; --i >= where; )         items[i+1] = items[i];     Py_INCREF(v);     items[where] = v;     return 0; } 
like image 187
Nir Alfasi Avatar answered Oct 02 '22 21:10

Nir Alfasi