"Canonical" way of deleting elements from lists

Hrvoje Niksic hniksic at xemacs.org
Wed Jan 9 11:52:13 CET 2008


Hrvoje Niksic <hniksic at xemacs.org> writes:

> If you're looking for a quick (no quadratic behavior) and convenient
> way to do it, you can do it like this:
>
> keywords = [s for s in keywords if s != '']

It now occurred to me that a good compromise between convenience and
efficiency that retains the same list is:

keywords[:] = (s for s in keywords if s)



More information about the Python-list mailing list