Given a list as follows, deduplicating the list without concern for order, we can do this:
from collections import OrderedDict
x = [1,2,3,9,8,3,2,5,3,2,7,3,6,7]
list(set(x)) # Outputs: [1, 2, 3, 5, 6, 7, 8, 9]
If we want to keep the order, we can do something like:
list(OrderedDict.fromkeys(x)) # Outputs: [1, 2, 3, 9, 8, 5, 7, 6]
Using OrderedDict.fromkeys
, then casting it into a list again is kept of cumbersome. And seem counter-intuitive but I suppose there's some reason for Python dev to decide on processing sets with ordering as dictionary instead.
Is there such an object as OrderedSet
that can achieve the same deduplication while keeping order?
If there isn't, what is the motivation to support OrderedDict
but not OrderedSet
? Is it an anti-pattern to create a custom object like OrderedSet
?