import queue.PriorityQueue # Create the queue. candidates = PriorityQueue() # Add a tuple (distance, node) to the queue. candidates.put((0, start)) # ... while candidates: # Pop the next candidate off the queue - it is the nearest # of all known candidates. distance, node = candidates.get()