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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'skipping': 0.07; 'python': 0.08; '[0,': 0.09; 'iterate': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'list?': 0.13; 'adjacent': 0.16; 'failed.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'somehow': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'subject:list': 0.21; "doesn't": 0.22; 'header :In-Reply-To:1': 0.22; 'feb': 0.22; 'accidentally': 0.23; 'cc:2**0': 0.26; 'function': 0.27; 'tried': 0.27; 'message- id:@mail.gmail.com': 0.29; 'odd': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'regardless': 0.31; 'values': 0.32; 'received:209.85.215.46': 0.32; 'list': 0.32; 'fri,': 0.34; 'test': 0.34; 'direction': 0.34; 'something': 0.35; 'list,': 0.36; 'two': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'correctly': 0.39; 'subject:from': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'your': 0.61; 'today,': 0.69; 'reverse': 0.73; 'keep.': 0.84; 'them:': 0.84; 'items,': 0.91; 'philips': 0.91; 'surprise,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=JymEIG7zv7cpxKOnZ4+XAv63LUyRlQMMFhGR8JgfxnU=; b=SJaD/ffv2U5KPF70WZFA/FI9XQbsYZ5xJOvt3FEMmhllnH3hAxsZTRm+3et57epxQn DIFwfGWTn/s9bzhcUKJ5ZCb72j/9Ln8M+EFJw5KStwpxjr4cX+uYqBSbqTSFPF1vRPpj g9XKg76TTpCy3DceGI+EoJSJZzr7b07MWHDsg= MIME-Version: 1.0 In-Reply-To: <7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com> References: <7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com> From: Ian Kelly Date: Fri, 10 Feb 2012 13:22:47 -0700 Subject: Re: Removing items from a list To: Thomas Philips Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328905402 news.xs4all.nl 6962 [2001:888:2000:d::a6]:35932 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20182 On Fri, Feb 10, 2012 at 1:04 PM, 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 - =A0does the remove() function somehow > allow the iteration to continue correctly even when items are removed > from the midde of the list? No. Your test works because you never attempt to remove two adjacent items, so the skipping of items doesn't end up mattering. Try the same thing, but print out the values as you iterate over them: >>> x =3D list(range(10)) >>> x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in x: ... print(i) ... if i % 2 =3D=3D 0: ... x.remove(i) ... 0 2 4 6 8 >>> x [1, 3, 5, 7, 9] Had you attempted to remove any of the odd numbers as well, it would have failed. Cheers, Ian