Created
July 1, 2014 13:49
-
-
Save adamcheasley/e1d237f7d2bc138ca282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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
Surely the idfun would be better as a default argument?