Deleting from a list (reprise)

Anthony Baxter anthony at interlink.com.au
Wed Jan 2 01:16:49 EST 2002


>>> Hans Nowak wrote
> >>> 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]
> >>> 

note that this is pretty inefficient, as it starts at the start of 
the list each time through the loop. I know my python hindbrain tends 
to prick up it's little ears whenever it sees my stupid frontbrain 
write code like "foo in listobj" to make sure I shouldn't be using 
something like a dictionary instead.

Anthony




More information about the Python-list mailing list