Question 9
Back to Exercise
The program below stops with an error. Which statement correctly describes the problem?
import heapq
tasks = []
heapq.heappush(tasks, (2, {"name": "deploy"}))
heapq.heappush(tasks, (2, {"name": "review"}))
print(heapq.heappop(tasks))
- A It prints (2, {'name': 'deploy'})
- B KeyError
- C IndexError
- D TypeError: equal priorities make heapq compare the dicts, which do not support <; add a counter as a tie-breaker: (priority, count, task) Correct
Correct Answer: Option D
Tuples compare item by item; a unique second item (an increasing counter) stops the comparison before it reaches the dict.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!