It looks like you need an ordered set instead of a regular set.
>>> x = [1, 5, 3, 4]
>>> y = [3]
>>> print(list(OrderedSet(x) - OrderedSet(y)))
[1, 5, 4]
Python doesn't come with an ordered set, but it is easy to make one:
import collections.abc
class OrderedSet(collections.abc.Set):
def __init__(self, iterable=()):
self.d = collections.OrderedDict.fromkeys(iterable)
def __len__(self):
return len(self.d)
def __contains__(self, element):
return element in self.d
def __iter__(self):
return iter(self.d)
Hope this helps :-)
sets
module. Use the builtinset
type. – Rattletrap