Delete all items in the list
clp2 at rebertia.com
Thu Feb 26 06:15:49 EST 2009
On Thu, Feb 26, 2009 at 3:05 AM, Clarendon <jineu21 at 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']
Follow the path of the Iguana...
More information about the Python-list