Same order unique list using only a list compression.
> my_list = [1, 2, 1, 3, 2, 4, 3, 5, 4, 3, 2, 3, 1]
> unique_list = [
> e
> for i, e in enumerate(my_list)
> if my_list.index(e) == i
> ]
> unique_list
[1, 2, 3, 4, 5]
enumerates
gives the index i
and element e
as a tuple
.
my_list.index
returns the first index of e
. If the first index isn't i
then the current iteration's e
is not the first e
in the list.
Edit
I should note that this isn't a good way to do it, performance-wise. This is just a way that achieves it using only a list compression.