๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ heapq.html
heapq โ€” Heap queue algorithm
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. Min-heaps are binary trees for which every parent node has a value less than or equal to any of its children.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ priority-queue-in-python
Priority Queue in Python - GeeksforGeeks
April 26, 2025 - Max Priority Queue: The element with the highest priority is dequeued first. Itโ€™s commonly used when you need to process the most important or largest element first. Min Priority Queue: The element with the lowest priority is dequeued first.
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ priority-queues-in-python
Introduction to Priority Queues in Python | Built In
PriorityQueue class: uses a Python class interface and supports thread safety; has O(log n) time complexity. The heapq module in Python provides a min heap data structure by default, where the root element of the heap is the smallest item.
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to implement a priority queue in python
How to implement a priority queue in Python | Replit
March 3, 2026 - Python's heapq module provides an efficient min-heap implementation, which is perfect for a priority queue. It operates directly on a list, using tuples like (priority, task) to organize data.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - A priority queue is a data structure ... In Python, you have several options for implementing priority queues: The heapq module provides a fast and memory-efficient implementation of a min-heap priority queue...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 73652715 โ€บ how-can-i-extract-the-minimum-value-of-a-priorityqueue
algorithm - How can I extract the minimum value of a PriorityQueue? - Stack Overflow
So I was working with priority queues, however, I never used them before and I do not know how to extract the minimum value of the priority queue. Can someone help me? Q = PriorityQueue() Q.put((start, dist[start])) while Q != 0: u = #the minimum value of PriorityQueue ... That depends on the programming language you use. Have you read the documentation on the implementation you use? Surely it should give examples on how to extract the minimum. ... That will be up to the library that you are using. For example in Python the most common library to use is heapq.
๐ŸŒ
Python Guides
pythonguides.com โ€บ priority-queue-in-python
Priority Queue in Python
December 12, 2025 - In Python, priority queues are often implemented using the heapq module, which provides an efficient min-heap data structure.
๐ŸŒ
Stackify
stackify.com โ€บ a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - While queue.PriorityQueue is a ... it an excellent choice for custom priority queue operations. A heap is a specialized binary tree that satisfies the heap property: Every parent node is smaller than or equal to its children for a min-heap....
Top answer
1 of 3
33

There is no such thing as a "most efficient priority queue implementation" in any language.

A priority queue is all about trade-offs. See http://en.wikipedia.org/wiki/Priority_queue

You should choose one of these two, based on how you plan to use it:

  • O(log(N)) insertion time and O(1) (findMin+deleteMin)* time, or
  • O(1) insertion time and O(log(N)) (findMin+deleteMin)* time

(* sidenote: the findMin time of most queues is almost always O(1), so here I mostly mean the deleteMin time can either be O(1) quick if the insertion time is O(log(N)) slow, or the deleteMin time must be O(log(N)) slow if the insertion time is O(1) fast. One should note that both may also be unnecessarily slow like with binary-tree based priority queues.)

In the latter case, you can choose to implement a priority queue with a Fibonacci heap: http://en.wikipedia.org/wiki/Heap_(data_structure)#Comparison_of_theoretic_bounds_for_variants (as you can see, heapq which is basically a binary tree, must necessarily have O(log(N)) for both insertion and findMin+deleteMin)

If you are dealing with data with special properties (such as bounded data), then you can achieve O(1) insertion and O(1) findMin+deleteMin time. You can only do this with certain kinds of data because otherwise you could abuse your priority queue to violate the O(N log(N)) bound on sorting. vEB trees kind of fall under a similar category, since you have a maximum set size (O(log(log(M)) is not referring to the number of elements, but the maximum number of elements) and thus you cannot circumvent the theoretical O(N log(N)) general-purpose comparison-sorting bound.

To implement any queue in any language, all you need is to define the insert(value) and extractMin() -> value operations. This generally just involves a minimal wrapping of the underlying heap; see http://en.wikipedia.org/wiki/Fibonacci_heap to implement your own, or use an off-the-shelf library of a similar heap like a Pairing Heap (a Google search revealed http://svn.python.org/projects/sandbox/trunk/collections/pairing_heap.py )


If you only care about which of the two you referenced are more efficient (the heapq-based code from http://docs.python.org/library/heapq.html#priority-queue-implementation-notes which you included above, versus Queue.PriorityQueue), then:

There doesn't seem to be any easily-findable discussion on the web as to what Queue.PriorityQueue is actually doing; you would have to source dive into the code, which is linked to from the help documentation: http://hg.python.org/cpython/file/2.7/Lib/Queue.py

Copy   224     def _put(self, item, heappush=heapq.heappush):
   225         heappush(self.queue, item)
   226 
   227     def _get(self, heappop=heapq.heappop):
   228         return heappop(self.queue)

As we can see, Queue.PriorityQueue is also using heapq as an underlying mechanism. Therefore they are equally bad (asymptotically speaking). Queue.PriorityQueue may allow for parallel queries, so I would wager that it might have a very slightly constant-factor more of overhead. But because you know the underlying implementation (and asymptotic behavior) must be the same, the simplest way would simply be to run them on the same large dataset.

(Do note that Queue.PriorityQueue does not seem to have a way to remove entries, while heapq does. However this is a double-edged sword: Good priority queue implementations might possibly allow you to delete elements in O(1) or O(log(N)) time, but if you use the remove_task function you mention, and let those zombie tasks accumulate in your queue because you aren't extracting them off the min, then you will see asymptotic slowdown which you wouldn't otherwise see. Of course, you couldn't do this with Queue.PriorityQueue in the first place, so no comparison can be made here.)

2 of 3
29

The version in the Queue module is implemented using the heapq module, so they have equal efficiency for the underlying heap operations.

That said, the Queue version is slower because it adds locks, encapsulation, and a nice object oriented API.

The priority queue suggestions shown in the heapq docs are meant to show how to add additional capabilities to a priority queue (such as sort stability and the ability to change the priority of a previously enqueued task). If you don't need those capabilities, then the basic heappush and heappop functions will give you the fastest performance.

Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
February 23, 2026 - If maxsize is less than or equal to zero, the queue size is infinite. The lowest valued entries are retrieved first (the lowest valued entry is the one that would be returned by min(entries)).
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-python-priority-queue
What is the Python priority queue?
Since the queue.PriorityQueue class needs to maintain the order of its elements, a sorting mechanism is required every time a new element is enqueued. Python solves this by using a binary heap to implement the priority queue.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ heap-and-priority-queue-using-heapq-module-in-python
Heap and Priority Queue using heapq module in Python - GeeksforGeeks
July 23, 2025 - The left child of a node in index ... queue is implemented in Python as a list of tuples where the tuple contains the priority as the first element and the value as the next element....
๐ŸŒ
Programiz
programiz.com โ€บ dsa โ€บ priority-queue
Priority Queue Data Structure
Extract-Max returns the node with maximum value after removing it from a Max Heap whereas Extract-Min returns the node with minimum value after removing it from Min Heap. ... # Priority Queue implementation in Python # Function to heapify the tree def heapify(arr, n, i): # Find the largest ...
๐ŸŒ
Medium
varun-verma.medium.com โ€บ priority-queue-heap-in-python-ef98d5042602
Priority Queue/Heap in Python. Priority Queues are an efficient way toโ€ฆ | by Verma Varun | Medium
October 26, 2024 - Priority Queues are an efficient way to get the min or max element from a list with O(1) time v/s using a min() function that loops through the entire list and gets the min or max element in O(n) time.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - This is a characteristic of Pythonโ€™s PriorityQueue, where lower numeric values represent higher priority. Hence, the element with the lowest value (1 in this case) is removed first.
๐ŸŒ
Wander In Dev
wanderin.dev โ€บ python-interview โ€บ a-priority-queue-implementation-in-python
A Priority Queue Implementation in Python โ€“ Wander In Dev
October 6, 2024 - While the next element in a queue ... one with the highest priority. ... In a min-priority queue, you assign a higher priority to a task with a lower priority number....
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-priority-queue
What is the Python Priority Queue? | Linode Docs
June 17, 2022 - In Python, queues are frequently used to process items using a first in first out (FIFO) strategy. However, it is often necessary to account for the priority of each item when determining processing order. A queue that retrieves and removes items based on their priority as well as their arrival time is called a priority queue.