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?
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.