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?
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))]
You could use a set like this:
newls = []
seen = set()
for elem in ls:
if not elem in seen:
newls.append(elem)
seen.add(elem)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With