On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <jine...@hotmail.com> 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?

There are several ways. I'd go with a list comprehension:

L = [i for i in L if i != 'a']

Or to modify the list in-place:

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

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to