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

mik3l3374 at gmail.com mik3l3374 at gmail.com
Wed Apr 4 11:20:00 CEST 2007


On Apr 4, 2:20 am, "bahoo" <b83503... at yahoo.com> wrote:
> Hi,
>
> I have a list like ['0024', 'haha', '0024']
> and as output I want ['haha']
>
> If I
> myList.remove('0024')
>
> then only the first instance of '0024' is removed.
>
> It seems like regular expressions is the rescue, but I couldn't find
> the right tool.
>
> Thanks!
> bahoo

how about this:
>>> target = "0024"
>>> l = ["0024", "haha", "0024", "0024", "sfs"]
>>> result = [ item for item in l if item != target]
>>> result
['haha', 'sfs']




More information about the Python-list mailing list