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

Steven D'Aprano steve at
Fri Apr 6 15:49:53 CEST 2007

On Wed, 04 Apr 2007 15:56:34 +0200, Hendrik van Rooyen wrote:

> Now how would one do it and preserve the original order?
> This apparently simple problem is surprisingly FOS...
> But list comprehension to the rescue :
>>>>[x for x in duplist if duplist.count(x) == 1]
> ['haha', 5, 6]
> *shakes head* duh... why does it take so long?

Because you are using Shlemiel the painter's algorithm:

Each time you call duplist.count(), you go back to the beginning
of the list and walk the entire list. You end up walking the list over
and over and over again.


More information about the Python-list mailing list