how to remove multiple occurrences of a string within a list?

Ayaz Ahmed Khan ayaz at dev.slash.null
Wed Apr 4 15:29:08 EDT 2007


"kyosohma" typed:

> If you want to get really fancy, you could do a list comprehension
> too:
> 
> your_list = ["0024","haha","0024"]
> new_list = [i for i in your_list if i != '0024']

Or, just:

In [1]: l = ["0024","haha","0024"]
In [2]: filter(lambda x: x != "0024", l)
Out[2]: ['haha']

-- 
Ayaz Ahmed Khan

Do what comes naturally now.  Seethe and fume and throw a tantrum.



More information about the Python-list mailing list