Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77913 > unrolled thread

Re: List insert at index that is well out of range - behaves like append that too SILENTLY

Started bydieter <dieter@handshake.de>
First post2014-09-16 07:58 +0200
Last post2014-09-16 07:58 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: List insert at index that is well out of range - behaves like append that too SILENTLY dieter <dieter@handshake.de> - 2014-09-16 07:58 +0200

#77913 — Re: List insert at index that is well out of range - behaves like append that too SILENTLY

Fromdieter <dieter@handshake.de>
Date2014-09-16 07:58 +0200
SubjectRe: List insert at index that is well out of range - behaves like append that too SILENTLY
Message-ID<mailman.14041.1410847105.18130.python-list@python.org>
Harish Tech <technews.full@gmail.com> writes:

> Let me demonstrate the problem I encountered :
>
> 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

At least the documentation states that what you observe is the intended
behaviour.

According to the documentation, "a.insert(i, x)" is
equivalent to "a[i:i] = x" (i.e. a slice assignment) and
if in a slice "a[i:j]" "i" or "j" are larger then "len(a)",
then it is replaced by "len(a)".

If this is not what you want, derive your own list type
and override its "insert" method.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web