class OrderedSet(collections.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)
Retaining order while using Python's set difference