Skip to content

Instantly share code, notes, and snippets.

@crmccreary
Created June 23, 2011 23:09
Show Gist options
  • Save crmccreary/1043855 to your computer and use it in GitHub Desktop.
Save crmccreary/1043855 to your computer and use it in GitHub Desktop.
uniquefy a list
def uniquefy(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment