unique items in lists

Remco Gerlich scarblac at pino.selwerd.nl
Tue Mar 27 05:09:23 EST 2001


comp.lang.python <micampe at f2s.com> wrote in comp.lang.python:
> Which is the best (read: fastest) way to remove duplicated items from a list
> of strings?

The standard idiom is to put the items in a dictionary, then get the
dictionary keys:

def unique_strings(l):
   dict = {}
   for s in l:
      dict[s] = 1
   return dict.keys()

I think this is also very fast, at least lots faster than comparing all the
strings in a double for loop or something.

-- 
Remco Gerlich



More information about the Python-list mailing list