๐ŸŒ
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.
Discussions

algorithm - How can I extract the minimum value of a PriorityQueue? - Stack Overflow
Given that people use priority queues for performance, pretty code might not be the win either of us would hope. Other languages, of course, are different. For example here is PriorityQueue in Java. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 2 How do I keep track of the shortest paths in the Dijkstra algorithm when using a min... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Priority queue with higher priority first in Python - Stack Overflow
I need a priority queue that gets the item with the highest priority value first. I'm currently using the PriorityQueue Class from the Queue library. However, this function only returns the items w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to implement Priority Queues in Python? - Stack Overflow
However this is a double-edged ... 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.) ... Sign up to request clarification or add additional context in comments. ... I undestand priority queue theoretically pretty well and thus the possible DS. But the question is about its implementation in Python which has ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to "decrease priority" in a min-priority queue in Dijkstra's algorithm?
This depends on how the priority queue is implemented, as there are several ways of achieving this in logarithmic time. For example, if implemented as a balanced BST (binary search tree), all lookups, insertions, and removals are logarithmic. Hence decrease priority could just be implemented as a removal followed by an insertion (of the old and new values). More on reddit.com
๐ŸŒ r/algorithms
11
18
January 10, 2022
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 ย  $$
Call ย  +44 1244 911212
Address ย  Chester Business Park, 220 Heronsway, CH4 9GB
๐ŸŒ
Blogboard
blogboard.io โ€บ blog โ€บ knowledge โ€บ priority-queue-in-python
Priority Queue in Python
April 9, 2024 - Items/Priorities ('a', 2) ('b', ... entry. That is priority 0 is higher than priority 1. Technically, PriorityQueue is implemented using a min-heap data ......