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

Maël Benjamin Mettler mbm at mediamonger.ch
Wed Apr 4 06:48:38 EDT 2007


How about:

list(frozenset(['0024', 'haha', '0024']))

mik3l3374 at gmail.com schrieb:
> 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