iterating over collection, deleting entries
Larry Bates
lbates at swamisoft.com
Mon Apr 26 15:48:30 EDT 2004
I would do:
collection=[i for i in collection if not isBad(i)]
Larry Bates
Syscon, Inc.
"Patrick von Harsdorf" <patrick at harsdorf.de> wrote in message
news:c6gop3$5p7$03$1 at news.t-online.com...
> I want to iterate over a collection and delete all unwanted entries.
>
> for item in collection:
> del item
>
> of course doesn´t do anything useful. Two things come to mind:
> a) iterating backwards over the collection using indexing, something like:
>
> for i in range(len(collection)-1, -1, -1):
> item = collection[i]
> if isBad(item) del collection[i]
>
> b) duplicating the collection, iterating over one and deleting from the
> other.
>
> Both solutions seem both ugly and expensive to me, solution a)
> probably isn´t even O(x) anymore.
>
> Please tell me, what´s the best way to do this?
>
>
> --
> Patrick von Harsdorf
> patrick at harsdorf.de
>
>
>
>
More information about the Python-list
mailing list