Eliminating duplicates entries from a list efficiently

Roy Smith roy at panix.com
Fri Jul 2 20:55:54 EDT 2004


In article <cc4v90$g5e$0 at 216.39.144.46>, Paul <paul at oz.net> 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
>     
> print uniq(['a','b','c','a'])
> ['a', 'b', 'c']
> 
> Thanks,
> 
> -- Paul

Something like:

d = {}
for item in list:
   d[item] = True
list = d.keys()

This should do it and will run in, if I'm not mistaken, O(n).  The only 
problem is that it scrambles the order of the items in the list, which 
may or may not be a problem, depending on your application.



More information about the Python-list mailing list