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
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
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.
>>>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
Besides the other answers ...
foo [] #bind 'foo' to empty list
foo[:] # delete in place
To selectively delete multiple items, use filter().
Terry J. Reedy
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/
> >>>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/
> >>> 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 --------------------------------------
------------------------------------------------------------------------
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
>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
>
>--
>http://mail.python.org/mailman/listinfo/python-list
>