I assume you are using heapq
. The documentation has this about it, which seems quite reasonable:
The remaining problems are related to finding a pending task and making changes to the priority or complete deletion. A task search can be done with a dictionary pointing to an entry in the queue.
Deleting a record or changing its priority is more difficult because it destroys the invariants of the heap structure. So, a possible solution is to mark the existing record as deleted and add a new record using the revised priority.
The documentation contains some basic code example showing how this can be done, which I reproduce here verbatim:
pq = []
source share