Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'insert': 0.05; 'assignment': 0.07; 'method.': 0.07; '[1,': 0.09; 'append': 0.09; 'override': 0.09; 'behaviour.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'indexerror:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject: \n ': 0.16; 'subject:too': 0.16; 'throw': 0.16; 'demonstrate': 0.16; 'index': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'header:User- Agent:1': 0.23; 'subject:like': 0.24; 'equivalent': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'errors': 0.30; 'originally': 0.30; 'subject:that': 0.31; 'writes:': 0.31; 'another': 0.32; '(i.e.': 0.33; 'problem': 0.35; 'received:84': 0.35; 'subject:List': 0.36; 'tech': 0.36; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'according': 0.40; 'even': 0.60; 'range': 0.61; 'special': 0.74; '100': 0.79; '"j"': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MK7umapl c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=XDEB89xunmkA:10 a=gCUDXwEN2uwA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=8CJqt8RZRI1EPFXXTdAA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 16 Sep 2014 13:04:57 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: List insert at index that is well out of range - behaves like append that too SILENTLY References: <8738bsds44.fsf@handshake.de> In-Reply-To: <8738bsds44.fsf@handshake.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410869105 news.xs4all.nl 2916 [2001:888:2000:d::a6]:56944 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77928 On 2014-09-16 06:58, dieter wrote: > Harish Tech 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. > It's nice to be able to say a.insert(p, x) even when p == len(a) instead of having a special case: if p == len(a): a.append(x) else: a.insert(p, x) Of course, what to do when p > len(a) is another matter.