Skip to content

Instantly share code, notes, and snippets.

@adamcheasley
Created July 1, 2014 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcheasley/e1d237f7d2bc138ca282 to your computer and use it in GitHub Desktop.
Save adamcheasley/e1d237f7d2bc138ca282 to your computer and use it in GitHub Desktop.
def uniquify_ordered(seq, idfun=None):
# Removes duplicates from a list and preserves the order of items
# Taken from http://www.peterbe.com/plog/uniqifiers-benchmark
if idfun is None:
def idfun(x):
return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if marker in seen:
continue
seen[marker] = 1
result.append(item)
return result
@adamcheasley
Copy link
Author

Surely the idfun would be better as a default argument?

def uniquify_ordered(seq, idfun=lambda x: x):

@adamcheasley
Copy link
Author

Even better, from mattr:

>>> a = [1, 1, 5, 2, 3, 4, 3, 2, 1, 4, 5]
>>> from collections import OrderedDict
from collections import OrderedDict
>>> list(OrderedDict.fromkeys(a))
list(OrderedDict.fromkeys(a))
[1, 5, 2, 3, 4]
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment