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


Groups > comp.lang.python > #20183

Re: Removing items from a list

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 <python@mrabarnett.plus.com>
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 <python@mrabarnett.plus.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5676.1328905774.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:04 -0800
  Re: Removing items from a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-10 13:22 -0700
    Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:48 -0800
  Re: Removing items from a list MRAB <python@mrabarnett.plus.com> - 2012-02-10 20:26 +0000
  Re: Removing items from a list Chris Angelico <rosuav@gmail.com> - 2012-02-11 08:58 +1100
    Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-13 05:55 -0800

csiph-web