🌐
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.
🌐
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.
🌐
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...
🌐
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.
🌐
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 value of the parent node in each level is less than or equal to its children's values - min-heap. The value of the parent node in each level higher than or equal to its children's values - max-heap.
🌐
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.
🌐
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 Queue/Heap in Python 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 …
🌐
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. ... Save this answer. ... Show activity on this post. That will be up to the library that you are using. For example in Python the most common library to use is heapq.
🌐
Medium
medium.com › @huawei.zhu › built-in-stack-queue-and-priority-queue-in-python-e44a6cbf3771
Built-in stack, queue and priority queue in Python | by Huawei Zhu | Medium
September 2, 2025 - Using queue.PriorityQueue: This approach supports concurrent processes and it’s a class interface. The 2nd one — heapq, is preferred. ... import heapq # also a built-in module in python, and this is min-heap.
Find elsewhere
🌐
Stackify
stackify.com › a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - Memory Usage: heapq is more memory efficient as it doesn’t require the overhead of managing a queue object. Minimize Conversions: When using heapq, ensure your data is already in a list to avoid conversion overhead. For example, directly push items onto the heap rather than creating a new heap from an existing list. Use Custom Key Functions: Whether you use PriorityQueue or heapq, consider using tuples or custom objects with a key function for priority.
🌐
Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
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)).
🌐
Plain English
python.plainenglish.io › how-to-implement-priority-queues-cc41d43e325b
Implementing Priority Queues | Python in Plain English
November 5, 2021 - heappop(heap) takes a list (which must be a heap) as input, and return the minimum element, which is also removed from the heap. heappush(heap, value) takes a list (a heap) and a value as input, and adds the value to the heap · So now we can easily create our priority queue in Python:
🌐
Scaler
scaler.com › home › topics › program for priority queue in python
Program for Priority Queue in Python - Scaler Topics
December 13, 2022 - To implement a priority queue, we can also use the heapq module in Python. Insertion and extraction of the smallest element take O(log n) time in this implementation. It should be noted that heapq only has a min heap implementation, however, there are ways to use it as a max heap as well.
🌐
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 ... value (1 in this case) is removed first. Python’s PriorityQueue operates using a data structure known as a min-heap....
🌐
Python Pool
pythonpool.com › home › tutorials › python priority queue: heapq vs queue.priorityqueue
Understanding Priority Queue in Python with Implementation
September 3, 2020 - In short, use heapq for fast ... when the largest priority should be processed first. heapq turns a list into a min-heap where the smallest priority is removed first....
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

   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.

🌐
Progressive Robot
progressiverobot.com › home › python › how to use a priority queue in python
Priority Queue: Complete Guide - Progressive Robot
May 12, 2026 - There are two common ways to implement a priority queue in Python: ... Python's heapq module implements a min-heap by default. This means that the smallest element (based on the priority) is always at the root of the heap.
Price   $$
Address   Chester Business Park, 220 Heronsway, CH4 9GB
🌐
Medium
medium.com › @balakrishnamaduru › heap-and-priority-queue-in-python-mastering-heapq-and-queue-priorityqueue-bdab14488b43
Heap and Priority Queue in Python: Mastering heapq and queue.PriorityQueue | by Balakrishna Maduru | Medium
November 23, 2024 - Efficient task scheduling, finding the smallest/largest elements, and maintaining order in priority-based tasks often require specialized data structures. Python provides heaps and priority queues through two powerful modules: heapq for lightweight, efficient heaps, and queue.PriorityQueue for thread-safe operations. ... The heapq module implements a min-heap in Python.