Deleting from a list (reprise)

Hans Nowak wurmy at earthlink.net
Wed Jan 2 05:15:30 CET 2002


bvdpoel at uniserve.com wrote:

> I ired:
> 
>         for l in lines:
>                 if...:
>                         lines.remove(l)
> 
> But, this buggers the sequence. If you have [1,2,3,6,6,6,7] and delete
> on the condition l==6, you only delete 2 '6's, not all 3.

If you want to remove all occurrences of a certain object/number
from the list, you can do something like the following:

>>> x = [1, 2, 3, 1, 2, 8, 3, 1, 1, 1, 6]
>>> while 1 in x:
	x.remove(1)
>>> x
[2, 3, 2, 8, 3, 6]
>>> 

-- 
--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==\n') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list