Is there a lineal python structure that preserves insertion order and uniqueness of elements at the same time? I know that sets preserve uniqueness and list insertion order. By now I will implement this behavior with a class like:
class OrderedUniqueContainer:
def __init__(self):
self._data = []
def add(self, object):
# Assuming object has neccesary __hash__ and __eq__
if object not in self._data:
self._data.append(object)
def remove(self, object):
try:
self._data.remove(object)
except ValueError:
pass
I also need to implement union and difference. Is there a built-in structure to achieve this behavior?