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

前端 未结 7 830
天命终不由人
天命终不由人 2020-12-11 00:37

I had a list

 a = [1, 2, 3]

when I did

a.insert(100, 100)

[1, 2, 3, 100]

as list was originally of siz

相关标签:
7条回答
  • 2020-12-11 01:10

    Perhaps the actual implementation will shed some light.

    static int
    ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
    {
        ...
        if (where > n)
            where = n;
        ...
    }
    

    So that answers the question of how.

    Philosophically, lists are not arrays, and there are many list manipulations that are tolerant of weird indexing. For instance, l[1:1000] will return [2,3]. This is all meant as a convenience to the programmer.

    0 讨论(0)
提交回复
热议问题