Emptying a list

Alex Martelli aleax at aleax.it
Sun Sep 16 10:19:33 EDT 2001


Ype Kingma wrote:
        ...
> When you need to remove some of the elements in a list you could
> also use list comprehensions, but these always create a new list:
> 
>     a = [i for i in a if not someRemoveCondition(i)]

Sure, but assigning to the whole-list-slice remedies that:

    a[:] = [i for i in a if not someRemoveCondition(i)]


Alex




More information about the Python-list mailing list