🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Source code: Lib/heapq.py · 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 › heap-and-priority-queue-using-heapq-module-in-python
Heap and Priority Queue using heapq module in Python - GeeksforGeeks
July 23, 2025 - The priority 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. ... Consider a simple priority queue implementation for scheduling the presentations ...
🌐
Built In
builtin.com › data-science › priority-queues-in-python
Introduction to Priority Queues in Python | Built In
We can also use the heapq module in Python to implement our priority queue. This implementation has O(log n) time complexity for insertion and extraction of the smallest element.
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - Concrete data structures implement ... heap implementation of the priority queue guarantees that both pushing (adding) and popping (removing) elements are logarithmic time operations....
🌐
DigitalOcean
digitalocean.com › community › tutorials › priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - A priority queue in Python is a data structure that allows elements to be added and removed based on their priority. It is a type of queue where each element is associated with a priority, and elements are removed in order of their priority. In Python, priority queues can be implemented using ...
🌐
The Python Coding Stack
thepythoncodingstack.com › p › python-heapq-heap-priority-queue
If You Love Queuing, Will You Also Love Priority Queuing? • [Club]
December 15, 2025 - It’s a tuple with two elements. The integer 2 refers to the Silver tier, which has the second priority level. Gold members get a 1 and Bronze members—you guessed it—a 3. But don’t use .append() to add Jim to service_queue. Instead, let’s use heapq.heappush() to push an item onto the heap:
🌐
Hostman
hostman.com › tutorials › implementing a priority queue in python
Implementing a Priority Queue in Python: A Comprehensive Guide
December 29, 2025 - Priority queues are widely used in various programming applications, including: ... Python provides a built-in library called heapq that can be used to implement priority queues.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Netalith
netalith.com › blogs › computer-science › python-heaps-priority-queues-heapq-guide
Python Heap Data Structure Guide: Mastering Heaps and Priority Queues with heapq
3 weeks ago - The core heapq methods for this are heappush and heappop. These fundamental functions operate in O(log N) time, providing the efficient element access required for dynamic data streams.
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › python-priority-queue-heapq
Python priority queue -- heapq - The Truth of Sisyphus - GitBook
To access the smallest item without popping it, use heap[0]. # keep a K-size priority queue (heapq in python), and always make it updated and return the smallest of this group, which will be the k-th large element def add(self, val): if ...
Find elsewhere
🌐
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 - In this article, you'll explore several implementation techniques and practical tips for Python. We'll also cover real-world applications and provide solutions for common bugs, so you can confidently use priority queues in your projects. import heapq pq = [] heapq.heappush(pq, (2, "Medium priority task")) heapq.heappush(pq, (1, "High priority task")) heapq.heappush(pq, (3, "Low priority task")) while pq: priority, task = heapq.heappop(pq) print(f"Processing {task} with priority {priority}")--OUTPUT--Processing High priority task with priority 1 Processing Medium priority task with priority 2 Processing Low priority task with priority 3
🌐
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 ... provides heaps and priority queues through two powerful modules: heapq for lightweight, efficient heaps, and queue.PriorityQueue for thread-safe operations....
Top answer
1 of 3
128

Queue.PriorityQueue is a thread-safe class, while the heapq module makes no thread-safety guarantees. From the Queue module documentation:

The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module.

The heapq module offers no locking, and operates on standard list objects, which are not meant to be thread-safe.

In fact, the PriorityQueue implementation uses heapq under the hood to do all prioritisation work, with the base Queue class providing the locking to make this thread-safe. See the source code for details.

This makes the heapq module faster; there is no locking overhead. In addition, you are free to use the various heapq functions in different, novel ways, the PriorityQueue only offers the straight-up queueing functionality.

2 of 3
30

queue.PriorityQueue is a partial wrapper around the heapq class.

In other words, a queue.PriorityQueue is actually a heapq, placed in the queue module with a couple of renamed methods to make the heapq easier to use, much like a regular queue.

In heapq, you use use the method heappush() to add a new item and the method heappop() to remove one. That is not very queue-like, so queue.PriorityQueue let you use the usual queue methods such as put and get to do the same thing.

There are some features of heapq that are not carried over into queue.PriorityQueue, such as heappushpop() and heapreplace(), but you are less likely to use those. If you need them (and I do in my current project), perhaps you should use heapq rather than queue.PriorityQueue.

Also, since heapq is specialized for its purpose, it is not thread safe (as noted in another answer here.)

🌐
Medium
medium.com › @2019077_13406 › priority-queue-v-s-heapq-python-a4858c0191ac
Priority Queue V/s Heapq Python. Lately, I have been solving LeetCode… | by kartik chuphal | Medium
December 15, 2024 - I chose Python because I use it at work and want to become more proficient while solving various data structure problems. While implementing a solution with a priority queue, I came across two different implementation options in Python. Let’s dive into what each of them offers. Let’s just test which is faster. import time from queue import PriorityQueue, Empty import heapq def p_queue(): prior_queue = PriorityQueue() jobs = [(x, f"This is item: {x}") for x in range(1, 10 ** 5 + 1)] for job in jobs: prior_queue.put(job) while 1: try: popped_item = prior_queue.get_nowait() except Empty: brea
🌐
Bogotobogo
bogotobogo.com › python › python_PriorityQueue_heapq_Data_Structure.php
Python Tutorial: Data Structure - Priority Queue & heapq - 2020
The heapq implements a min-heap sort algorithm suitable for use with Python's lists. This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
🌐
Towards Data Science
towardsdatascience.com › home › latest › priority queues in python
Priority Queues in Python | Towards Data Science
January 26, 2025 - Next, let’s define a method that will allow us to push objects into our priority queue. We employ the ‘heappush’ method which will take the priority and the item value: class PriorityQueue: ... def push(self, item, priority): heapq.heappush(self._queue, (priority, self._index, item)) self._index += 1
🌐
CodeSignal
codesignal.com › learn › courses › interview-prep-the-last-mile-in-python › lessons › heap-and-priority-queue-in-python
Heap and Priority Queue in Python
For example, if you want to find the n-th largest number in a list, using sorting can be costly. By leveraging Python's heapq.nlargest function, the heap data structure lets us do this efficiently. ... Priority queues are an abstraction over heaps that store elements according to their priorities.
🌐
Get SDE Ready
getsdeready.com › heap-queues-in-python
Mastering Heap Queues in Python: A Guide to heapq
May 23, 2025 - Start incorporating heap queues into your Python projects to optimize performance and streamline your code! A heap queue prioritizes elements based on their value (smallest first in a min-heap), while a regular queue follows FIFO (First-In-...
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
A heap queue (also called a priority queue) is a data structure that allows quick access to the smallest (min-heap) or largest (max-heap) element. By default, heaps are implemented as min-heaps.
Published   2 weeks ago
🌐
Python Guides
pythonguides.com › priority-queue-in-python
Priority Queue in Python
December 12, 2025 - In many real-world applications ... priorities can change. Optimize performance with O(log n) insertion and removal. The heapq module is the go-to solution in Python for priority queues....
🌐
Runebook.dev
runebook.dev › en › docs › python › library › heapq › basic-examples
Troubleshooting Python's Priority Queue (heapq) with Practical Code Examples
import heapq # Priority is the first element (lower number = higher priority) priority_queue = [] heapq.heappush(priority_queue, (2, "Task B (Medium)")) heapq.heappush(priority_queue, (1, "Task A (High)")) heapq.heappush(priority_queue, (3, "Task C (Low)")) print(f"Priority Queue: {priority_queue}") # Output: [(1, 'Task A (High)'), (2, 'Task B (Medium)'), (3, 'Task C (Low)')] # Pop gets the highest priority task (lowest number) priority, task = heapq.heappop(priority_queue) print(f"Next task to execute: {task} (Priority: {priority})") # Output: Task A (High) (Priority: 1)