In Python, we can get the unique items of a list L
by using set(L)
. However doing this breaks the order in which the values appear in the original list. Is there an elegant way to get the unique items in the order in which they appear in the list?
Asked
Active
Viewed 1,224 times
5

wim
- 338,267
- 99
- 616
- 750

rambalachandran
- 2,091
- 2
- 19
- 34
2 Answers
10
If all of the items in the list are hashable, then a dictionary can be used for an order-preserving dedupe:
L = list(dict.fromkeys(L))
For older Python versions (<= 3.6) where dictionaries don't preserve ordering, you can do the same thing using a collections.OrderedDict
.
If any of the list items are unhashable, there will be a TypeError
. You can use an alternative approach in this case, although the complexity will degrade from O(n) to O(n^2). I refer you to the answer from Patrick Haugh.

wim
- 338,267
- 99
- 616
- 750
-
Note that your non-hashable update is already present in an answer from earlier, so it's a bit redundant in this answer. – Andras Deak -- Слава Україні Sep 28 '16 at 19:02
1
l = []
for item in list_:
if item not in l:
l.append(item)
This gets slow for really big, diverse list_
. In those cases, it would be worth it to also keep track of a set of seen values.

Patrick Haugh
- 59,226
- 13
- 88
- 96