Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

list remove

1 view
Skip to first unread message

James_...@i2.com

unread,
Oct 2, 2001, 12:57:38 PM10/2/01
to steven...@level3.com, pytho...@python.org

The python tutorial at
http://www.python.org/doc/current/tut/node7.html#SECTION00710000000000000000

0 has the section more on lists where it gives the function remove(x). I
am
trying to use this function to delete elements out of my list, but it only
deletes every other list.

>>>foo = [1, 2, 3, 4, 5]
>>>for bar in foo:
... foo.remove(bar)

>>>foo
[2, 4]

Is this an error on my part, pythons list type or am I misinterpretting
what
remove does? I have tried this on both 2.1 and 1.5.2.

Thanks.
Steve

================

It's not a good practice to delete items from a list when iterating over
that same list. Iterate over a copy of the list instead.

Just change

for bar in foo:

to

for bar in foo[:]: # now you are iterating over a copy of foo

Jim

Cliff Wells

unread,
Oct 2, 2001, 12:50:10 PM10/2/01
to pytho...@python.org
On Tuesday 02 October 2001 09:34, Oleg Broytmann wrote:
>
> NEVER, I said NEVER modify the list while running through it!
>

Yes, sort of like pulling the carpet you are standing on.

--
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308

Oleg Broytmann

unread,
Oct 2, 2001, 12:34:27 PM10/2/01
to steven...@level3.com, pytho...@python.org
On Tue, Oct 02, 2001 at 10:30:51AM -0600, steven...@LEVEL3.com wrote:
> The python tutorial at
> http://www.python.org/doc/current/tut/node7.html#SECTION00710000000000000000
> 0 has the section more on lists where it gives the function remove(x). I am
> trying to use this function to delete elements out of my list, but it only
> deletes every other list.
>
> >>>foo = [1, 2, 3, 4, 5]
> >>>for bar in foo:
> ... foo.remove(bar)
>
> >>>foo
> [2, 4]
>
> Is this an error on my part, pythons list type or am I misinterpretting what
> remove does? I have tried this on both 2.1 and 1.5.2.

NEVER, I said NEVER modify the list while running through it!

Oleg.
----
Oleg Broytmann http://phd.pp.ru/ p...@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.

steven...@level3.com

unread,
Oct 2, 2001, 12:30:51 PM10/2/01
to pytho...@python.org
The python tutorial at
http://www.python.org/doc/current/tut/node7.html#SECTION00710000000000000000
0 has the section more on lists where it gives the function remove(x). I am
trying to use this function to delete elements out of my list, but it only
deletes every other list.

>>>foo = [1, 2, 3, 4, 5]
>>>for bar in foo:
... foo.remove(bar)

>>>foo
[2, 4]

Is this an error on my part, pythons list type or am I misinterpretting what
remove does? I have tried this on both 2.1 and 1.5.2.

Thanks.
Steve

Terry Reedy

unread,
Oct 2, 2001, 1:56:16 PM10/2/01
to
> 0 has the section more on lists where it gives the function
remove(x). I am
> trying to use this function to delete elements out of my list, but
it only
> deletes every other list.
>
> >>>foo = [1, 2, 3, 4, 5]
> >>>for bar in foo:
> ... foo.remove(bar)
>
> >>>foo
> [2, 4]

Besides the other answers ...

foo [] #bind 'foo' to empty list
foo[:] # delete in place

To selectively delete multiple items, use filter().

Terry J. Reedy

Skip Montanaro

unread,
Oct 2, 2001, 1:21:44 PM10/2/01
to Oleg Broytmann, steven...@level3.com, pytho...@python.org

Oleg> NEVER, I said NEVER modify the list while running through it!

Never say "never". ;-)

This works just fine:

>>> foo = [1,2,3,4,5]
>>> for i in range(len(foo)-1,-1,-1):
... del foo[i]
...
>>> foo
[]

--
Skip Montanaro (sk...@pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/

Paul Sidorsky

unread,
Oct 2, 2001, 1:41:30 PM10/2/01
to pytho...@python.org
steven...@LEVEL3.com wrote:

> >>>foo = [1, 2, 3, 4, 5]
> >>>for bar in foo:
> ... foo.remove(bar)
>
> >>>foo
> [2, 4]

> Is this an error on my part, pythons list type or am I misinterpretting what


> remove does? I have tried this on both 2.1 and 1.5.2.

Each time an item is removed, the others get shifted up one. So what is
essentially happening is this:

>>> foo = [1, 2, 3, 4, 5]

>>> i = 0
>>> foo


[1, 2, 3, 4, 5]

>>> foo[i]
1
>>> foo.remove(foo[i])
>>> i += 1
>>> foo
[2, 3, 4, 5]
>>> foo[i]
3
>>> foo.remove(foo[i])
>>> i += 1
>>> foo
[2, 4, 5]
>>> foo[i]
5
>>> foo.remove(foo[i])
>>> i += 1
>>> foo
[2, 4]

Possible solutions are to either remove going from back-to-front (using ranges)
or to use a while loop and maintain your own index counter, incrementing it
only each time you find something you don't want to delete.

--
======================================================================
Paul Sidorsky Calgary, Canada
pau...@home.com http://members.home.net/paulsid/

Chris Barker

unread,
Oct 2, 2001, 6:21:32 PM10/2/01
to
Skip Montanaro wrote:
> Oleg> NEVER, I said NEVER modify the list while running through it!
>
> Never say "never". ;-)

> >>> foo = [1,2,3,4,5]


> >>> for i in range(len(foo)-1,-1,-1):
> ... del foo[i]
> ...
> >>> foo
> []

But this doesn't modify the list you are running through, it modifies
foo, while you are running through the list generated by the range().

-Chris


--
Christopher Barker,
Ph.D.
ChrisH...@home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------

chajadan

unread,
Oct 4, 2001, 1:20:58 AM10/4/01
to pytho...@python.org
This one really tickled me =)

But I actually was left with a question as well. When you say:

for bar in foo:

is not the first bar [1], so why doesn't the second element get deleted first? rather than being left at the end altogether..??

--chajadan

---------- Original Message ----------------------------------
From: steven...@LEVEL3.com
Date: Tue, 2 Oct 2001 10:30:51 -0600

>0 has the section more on lists where it gives the function remove(x). I am
>trying to use this function to delete elements out of my list, but it only
>deletes every other list.
>

>>>>foo = [1, 2, 3, 4, 5]
>>>>for bar in foo:
>... foo.remove(bar)
>
>>>>foo
>[2, 4]
>
>Is this an error on my part, pythons list type or am I misinterpretting what
>remove does? I have tried this on both 2.1 and 1.5.2.
>

>Thanks.
>Steve
>
>--
>http://mail.python.org/mailman/listinfo/python-list
>

0 new messages