If you want to keep order of the original list, just use OrderedDict with None
as values.
In Python2:
from collections import OrderedDict
from itertools import izip, repeat
unique_list = list(OrderedDict(izip(my_list, repeat(None))))
In Python3 it's even simpler:
from collections import OrderedDict
from itertools import repeat
unique_list = list(OrderedDict(zip(my_list, repeat(None))))
If you don't like iterators (zip and repeat) you can use a generator (works both in 2 & 3):
from collections import OrderedDict
unique_list = list(OrderedDict((element, None) for element in my_list))
No comments:
Post a Comment