[Tutor] removing items from a list

Galen O'Neil goneil@scu.edu
12 Oct 2002 00:42:31 -0700


Dear listreaders,
I want to remove items from a list that have content that matches my
data.  I can think of a few ways to do this, but none that seem as
simple as it should be.

This doesn't work, I'm not sure why.  Mabye item isn't a refence to the
list item, but just has the same value.

for item in list:
	if item == data: del item

This works, its ok, but still seems like there should be something
cleaner if not faster.
i=0
for item in list:
	if item == data:
		del list[i]
	i=i+1


This seems inefficient:

i=0
for item in list:
	if not item == data:
		newlist[i] = item
		i = i + 1
list = newlist

--Galen