def __delitem__(self, key): """ This is not O(1) because we have to traverse the heap to find the item to delete and then reheapify! """ value = obsdict.pop(self, key) cmpval = self._sortkey(value) if self._sortkey else value heapitem = (cmpval, key) self._heap.remove(heapitem) heapify(self._heap)
def popitem(self): """ Removes and returns the (key, value) pair with smallest sorted value if available. """ if not self: raise KeyError removed = heappop(self._heap) key = removed[1] value = obsdict.pop(self, key) return key, value