We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

How do you remove duplicates from a list?

See the Python Cookbook for a long discussion of many ways to do this:


If you don’t mind reordering the list, and all items can be used as dictionary keys (i.e. they are all hashable), you can simply do:

L = list(set(L))

or, for early Python versions:

d = {}
for x in L:
    d[x] = None
L = d.keys()

For unhashable objects, you can sort the sequence and then scan from the end of the list, deleting duplicates as you go:

if L:
   last = L[-1]
   for i in range(len(L)-2, -1, -1):
       if last == L[i]:
           del L[i]
           last = L[i]

CATEGORY: programming