Eliminating duplicates entries from a list efficiently

Gerhard Häring gh at ghaering.de
Sat Jul 3 02:51:25 CEST 2004


Paul wrote:
> Can anyone suggest an efficient way to eliminate duplicate entries
> in a list?  The naive approach below works fine, but is very slow 
> with lists containing tens of thousands of entries:
> 
> def uniq(list):
>     u = []
>     for x in list:
>         if x not in u: u.append(x)
>     return u [...]

That's slow for large lists. Better use a dictionary u. Also it's unwise 
to reuse builtin names like "list" as variable names.

Many discussions of this can be found in the mailing list/newsgroup 
archives via http://groups.google.com I also remember some neat 
solutions for this problem in the Python cookbook.

-- Gerhard




More information about the Python-list mailing list