I encounter many tasks in which I need to filter python (2.7) list to keep only ordered unique values. My usual approach is by using odereddict
from collections:
from collections import OrderedDict
ls = [1,2,3,4,1,23,4,12,3,41]
ls = OrderedDict(zip(ls,['']*len(ls))).keys()
print ls
the output is:
[1, 2, 3, 4, 23, 12, 41]
is there any other state of the art method to do it in Python?
- Note - the input and the output should be given as
list
edit - a comparison of the methods can be found here: https://www.peterbe.com/plog/uniqifiers-benchmark
the best solution meanwhile is:
def get_unique(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]