Delete all items in the list

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Feb 26 07:48:15 EST 2009

Clarendon a écrit :
> Hi. This must be a simple command but I just can't find it in the
> Phthon manual. How do I delete all items with a certain condition from
> a list? For instance:
> L=['a', 'b', 'c', 'a']
> I want to delete all 'a's from the list.
> But if L.remove('a') only deletes the first 'a'.
> How do you delete all 'a's?

L[:] = [item for item in L if item != 'a']

More information about the Python-list mailing list