so I have this list: a = [-11, 13, 13, 10, -11, 10, 9, -3, 6, -9, -6, -6, 13, 8, -11, -5, 6, -8, -12, 5, -9, -1, -5, 2, -2, 13, 14, -9, 7, -4]
and by using a set I need to remove the duplicates and also keep them in the same order
I used this code:
def unique(a):
a = set(a)
return list(a)
it does remove the duplicates when I use it but the problem is it returns them in numerical order like this:
>>> unique(a)
[-2, 2, 5, 6, 7, 8, 9, 10, 13, 14, -12, -11, -9, -8, -6, -5, -4, -3, -1]
how can I return it in the same order as the original list while removing duplicates using sets?
EDIT:
so I've used this code because it worked:
def unique(a):
seen = set()
return [seen.add(x) or x for x in a if x not in seen]
but can someone explain to me what it does? because I need to make another once but it returns the list without negative numbers, and I can't do that unless I understand what that code does