class bidict(dict):

    def __init__(self, *args, **kwargs):
        super(bidict, self).__init__(*args, **kwargs)
self.inverse = {}
for key, value in self.items():
    self.inverse.setdefault(value, []).append(key)

def __setitem__(self, key, value):
    super(bidict, self).__setitem__(key, value)
self.inverse.setdefault(value, []).append(key)
Efficient bidirectional hash table in Python?