Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert Python list to ordered unique values [duplicate]

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))]
like image 905
Dimgold Avatar asked Sep 10 '25 15:09

Dimgold


1 Answers

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)
like image 76
Eugene Yarmash Avatar answered Sep 13 '25 06:09

Eugene Yarmash