Delete all items in the list

Chris Rebert clp2 at
Fri Feb 27 07:59:54 CET 2009

On Thu, Feb 26, 2009 at 10:26 PM, odeits <odeits at> wrote:
> On Feb 26, 3:05 am, Clarendon <jine... at> wrote:
>> 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?
>> I would really appreciate your help.
>> Thanks.
> while 'a' in L:
>   L.remove('a')
> not the most efficient but it works

"Not the most efficient"; it's terribly inefficient! It's 2*O(M*N) [M
= number of 'a's in L], versus just N. And that's not even accounting
for all the extra element movement done by .remove()


Follow the path of the Iguana...

More information about the Python-list mailing list