Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'python': 0.08; '>>>>': 0.09; '[0,': 0.09; 'from:addr:python': 0.09; 'list?': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'is",': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply- to:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'somehow': 0.18; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'accidentally': 0.23; 'function': 0.27; 'tried': 0.27; 'regardless': 0.31; 'does': 0.32; 'list': 0.32; 'header:User- Agent:1': 0.33; 'direction': 0.34; 'received:84': 0.34; 'reply- to:addr:python.org': 0.34; 'to:addr:python-list': 0.35; 'something': 0.35; 'list,': 0.36; 'correctly': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'today,': 0.69; 'header:Reply-To:1': 0.70; 'reply-to:no real name:2**0': 0.72; 'reverse': 0.73; 'keep.': 0.84; 'philips': 0.91; 'surprise,': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=G8ee4qY5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=FFsdPcU1_MIA:10 a=aiHl1uDqG_QA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=cyjOKKJgCAbOsJ3yRNAA:9 a=03BWfeo8PO8m1ZYE4BAA:7 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 10 Feb 2012 20:26:26 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Removing items from a list References: <7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com> In-Reply-To: <7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328905774 news.xs4all.nl 6929 [2001:888:2000:d::a6]:43387 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20183 On 10/02/2012 20:04, Thomas Philips wrote: > In the past, when deleting items from a list, I looped through the > list in reverse to avoid accidentally deleting items I wanted to keep. > I tried something different today, and, to my surprise, was able to > delete items correctly, regardless of the direction in which I looped, > in both Python 3.2.2. and 2..1 - does the remove() function somehow > allow the iteration to continue correctly even when items are removed > from the midde of the list? > >>>> x = list(range(10)) >>>> x > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>> for i in x: > if i % 2 == 0: > x.remove(i) > >>>> x > [1, 3, 5, 7, 9] >>>> for i in reversed(x): > if i % 2 == 0: > x.remove(i) > >>>> x > [1, 3, 5, 7, 9] >>>> x = list(range(10)) >>>> for i in reversed(x): > if i % 2 == 0: > x.remove(i) > > >>>> x > [1, 3, 5, 7, 9] > The answer is no. For example: >>> for i in x: print("i is", i) if i % 2 == 0: x.remove(i) i is 0 i is 1 i is 2 i is 4 >>> x [0, 1, 3, 5]