Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
Created February 7, 2013 21:28
Show Gist options
  • Save lambdamusic/4734404 to your computer and use it in GitHub Desktop.
Save lambdamusic/4734404 to your computer and use it in GitHub Desktop.
Django: Remove duplicates from a list
def remove_duplicates(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
>>> a=list('ABeeE')
>>> f5(a)
['A','B','e','E']
>>> f5(a, lambda x: x.lower())
['A','B','e']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment