List without duplicates?

William Dandreta wjdandreta at worldnet.att.net
Tue Jun 20 18:36:37 EDT 2000


Is there a built in Python function that eliminates duplicates in a list?

I have been sorting the list and compairing adjacent elements and if equal
deleting one:
-------------------------------
mylist.sort()
    j = len(mylist)-1
    while j > 0:
      if mylist[j] == mylist[j-1]:
        del mylist[j]
      j = j - 1
------------------------------------
I recently tried using a dictionary. The list is the keys and I put and
empty string for the entry
--------------------
mydict = {}
for e in mylist:
  if mydict.has_key(e): continue
  else: mydict[e] = ''
------------------------------------
Is ther a better way?

Bill





More information about the Python-list mailing list