iterating through lists to delete elements
Mitch Chapman
chapman at bioreason.com
Thu Aug 9 12:53:10 EDT 2001
Erik Max Francis wrote:
>
> Mark Robinson wrote:
>
> > Can anyone advise me the best way to iterate through a list and
> > deleting
> > elements that don't meet a certain criteria.
>
> Use filter to make a new list, and then replace the original list with
> the filtered one.
>
> Modifying a list in place while you're iterating over it isn't a very
> good idea.
Another variation is to iterate across a copy while removing from
the original list.
for x in list[:]:
if x > y:
list.remove(x)
--
Mitch Chapman
Mitch.Chapman at bioreason.com
More information about the Python-list
mailing list