🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
heapq.heapreplace(heap, item)¶ · Pop and return the smallest item from the heap, and also push the new item. The heap size doesn’t change. If the heap is empty, IndexError is raised.
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-queue-or-heapq-in-python
Heap queue or heapq in Python - GeeksforGeeks
Note: The heapq module allows in-place heap operations on lists, making it an efficient and simple way to implement priority queues and similar structures.
Published   3 weeks ago
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - There are three rules that determine the relationship between the element at the index k and its surrounding elements: Its first child is at 2*k + 1. Its second child is at 2*k + 2. Its parent is at (k - 1) // 2. Note: The // symbol is the integer ...
🌐
Medium
cleverzone.medium.com › exploring-pythons-heapq-module-b0c9d131545c
Exploring Python’s heapq Module
June 17, 2024 - It provides a highly efficient way to manage data that requires frequent insertion and extraction of the smallest element. Key operations are performed in logarithmic time, making heapq suitable for a wide range of applications.
🌐
DEV Community
dev.to › devasservice › understanding-pythons-heapq-module-1n37
Understanding Python's heapq Module - DEV Community
September 19, 2024 - The heapq module provides functions to perform heap operations on a regular Python list.
🌐
CodeSignal
codesignal.com › learn › courses › understanding-and-using-trees-in-python › lessons › unraveling-heaps-theory-operations-and-implementations-in-python
Theory, Operations, and Implementations in Python
The heapify function transforms our list into a min-heap, and we continue extracting the minimum element until our heap becomes empty, resulting in a sorted list. Let's see how Python's built-in heapq module simplifies heap-sort: ... Exceptional progress, learners! You've made it through heaps, their operations, and their Python implementation.
🌐
W3Schools
w3schools.com › python › ref_module_heapq.asp
Python heapq Module
The heapq module provides heap (priority queue) algorithms on regular Python lists.
🌐
MicroPython
docs.micropython.org › en › latest › library › heapq.html
heapq – heap queue algorithm — MicroPython latest documentation
A heap queue is essentially a list that has its elements stored in such a way that the first item of the list is always the smallest. ... Push the item onto the heap. ... Pop the first item from the heap, and return it. Raise IndexError if heap is empty. The returned item will be the smallest ...
Find elsewhere
🌐
Real Python
realpython.com › ref › stdlib › heapq
heapq | Python Standard Library – Real Python
>>> import heapq >>> tasks = [(3, "write code"), (1, "write specs"), (2, "test code")] >>> heapq.heapify(tasks) >>> while tasks: ... priority, task = heapq.heappop(tasks) ... print(f"Processing task: {task} with priority {priority}") ...
🌐
Python Cheat Sheet
pythonsheets.com › notes › basic › python-heap.html
Heap — Python Cheat Sheet
>>> import heapq >>> # Convert list to heap in-place >>> h = [5, 1, 3, 2, 6] >>> heapq.heapify(h) >>> h[0] # smallest element at root 1 >>> # Push and pop >>> heapq.heappush(h, 0) >>> heapq.heappop(h) 0 >>> # Push and pop in one operation >>> heapq.heappushpop(h, 4) # push 4, then pop smallest 1 >>> # Pop and push in one operation >>> heapq.heapreplace(h, 0) # pop smallest, then push 0 2
Top answer
1 of 4
118

The heapq module maintains the heap invariant, which is not the same thing as maintaining the actual list object in sorted order.

Quoting from the heapq documentation:

Heaps are binary trees for which every parent node has a value less than or equal to any of its children. This implementation uses arrays for which heap[k] <= heap[2*k+1] and heap[k] <= heap[2*k+2] for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that its smallest element is always the root, heap[0].

This means that it is very efficient to find the smallest element (just take heap[0]), which is great for a priority queue. After that, the next 2 values will be larger (or equal) than the 1st, and the next 4 after that are going to be larger than their 'parent' node, then the next 8 are larger, etc.

You can read more about the theory behind the datastructure in the Theory section of the documentation. You can also watch this lecture from the MIT OpenCourseWare Introduction to Algorithms course, which explains the algorithm in general terms.

A heap can be turned back into a sorted list very efficiently:

def heapsort(heap):
    return [heapq.heappop(heap) for _ in range(len(heap))]

by just popping the next element from the heap. Using sorted(heap) should be faster still, however, as the TimSort algorithm used by Python’s sort will take advantage of the partial ordering already present in a heap.

You'd use a heap if you are only interested in the smallest value, or the first n smallest values, especially if you are interested in those values on an ongoing basis; adding new items and removing the smallest is very efficient indeed, more so than resorting the list each time you added a value.

2 of 4
41

Your book is wrong! As you demonstrate, a heap is not a sorted list (though a sorted list is a heap). What is a heap? To quote Skiena's Algorithm Design Manual

Heaps are a simple and elegant data structure for efficiently supporting the priority queue operations insert and extract-min. They work by maintaining a partial order on the set of elements which is weaker than the sorted order (so it can be efficient to maintain) yet stronger than random order (so the minimum element can be quickly identified).

Compared to a sorted list, a heap obeys a weaker condition the heap invariant. Before defining it, first think why relaxing the condition might be useful. The answer is the weaker condition is easier to maintain. You can do less with a heap, but you can do it faster.

A heap has three operations:

  1. Find-Minimum is O(1)
  2. Insert O(log n)
  3. Remove-Min O(log n)

Crucially Insert is O(log n) which beats O(n) for a sorted list.

What is the heap invariant? "A binary tree where parents dominate their children". That is, "p ≤ c for all children c of p". Skiena illustrates with pictures and goes on to demonstrate the algorithm for inserting elements while maintaining the invariant. If you think a while, you can invent them yourself. (Hint: they are known as bubble up and bubble down)

The good news is that batteries-included Python implements everything for you, in the heapq module. It doesn't define a heap type (which I think would be easier to use), but provides them as helper functions on list.

Moral: If you write an algorithm using a sorted list but only ever inspect and remove from one end, then you can make the algorithm more efficient by using a heap.

For a problem in which a heap data structure is useful, read https://projecteuler.net/problem=500

🌐
Developer-service
developer-service.blog › understanding-pythons-heapq-module
Understanding Python's heapq Module
September 19, 2024 - import heapq heap = [] heapq.heappush(heap, 10) heapq.heappush(heap, 5) heapq.heappush(heap, 20) After these operations, heap will be [5, 10, 20], with the smallest element at index 0. The smallest element can be accessed without removing it ...
🌐
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 - But don’t use .append() to add Jim to service_queue. Instead, let’s use heapq.heappush() to push an item onto the heap:
🌐
Medium
dpythoncodenemesis.medium.com › understanding-pythons-heapq-module-a-guide-to-heap-queues-cfded4e7dfca
Understanding Python’s Heapq Module: A Guide to Heap Queues | by Python Code Nemesis | Medium
October 21, 2023 - This process is optimized due to the logarithmic nature of the binary heap structure, resulting in the overall time complexity of O(log n) for both heappush and heappop operations. Let’s demonstrate the time complexities with Python examples. import heapq # Creating a simple heap heap = [] heapq.heappush(heap, 4) heapq.heappush(heap, 1) heapq.heappush(heap, 7) heapq.heappush(heap, 3) print("Heap after push operations:", heap)
🌐
FavTutor
favtutor.com › blogs › heapq-python
Python's heapq module: Implementing heap queue algorithm
May 4, 2023 - Depending on the size and structure ... of Python's sorting options, heapq, and sort, varies. Sort is typically more efficient than heapq for processing small arrays and lists. This is due to the fact that sort makes use of the cache memory available on modern CPUs and employs a highly optimized algorithm designed specifically for working with small lists. However, heapq outperforms sort as the list or array size increases. This is because heapq uses a heap data structure, and operations such as adding ...
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_heaps.htm
Python - Heaps
import heapq H = [21,1,45,78,3,5] # Covert to a heap heapq.heapify(H) print(H) # Add element heapq.heappush(H,8) print(H)
🌐
Educative
educative.io › answers › what-is-the-heapqheappop-method-in-python
What is the heapq.heappop() method in Python?
Note: Refer to What is a Heap? and What is the Python priority queue? to understand more about heaps and priority queues. The heappop method pops and returns the smallest element of the given heap. This method removes the smallest element.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-heapq-heappop-method
Python heapq.heappop() Method - GeeksforGeeks
July 23, 2025 - The heapq.heappop() function in Python is used to pop and return the smallest element from a heap, maintaining the heap property.
🌐
Educative
educative.io › answers › what-is-the-heapqheapify-module-in-python
What is the heapq.heapify() module in Python?
The heapq module is an inbuilt module in Python that offers APIs for different operations of the heap data structure. The module provides min-heap implementation where the key of the parent is less than or equal to those of its children.
Top answer
1 of 6
98

heapq is a binary heap, with O(log n) push and O(log n) pop. See the heapq source code.

The algorithm you show takes O(n log n) to push all the items onto the heap, and then O((n-k) log n) to find the kth largest element. So the complexity would be O(n log n). It also requires O(n) extra space.

You can do this in O(n log k), using O(k) extra space by modifying the algorithm slightly. I'm not a Python programmer, so you'll have to translate the pseudocode:

# create a new min-heap
# push the first k nums onto the heap
for the rest of the nums:
    if num > heap.peek()
        heap.pop()
        heap.push(num)

# at this point, the k largest items are on the heap.
# The kth largest is the root:

return heap.pop()

The key here is that the heap contains just the largest items seen so far. If an item is smaller than the kth largest seen so far, it's never put onto the heap. The worst case is O(n log k).

Actually, heapq has a heapreplace method, so you could replace this:

    if num > heap.peek()
        heap.pop()
        heap.push(num)

with

    if num > heap.peek()
        heap.replace(num)

Also, an alternative to pushing the first k items is to create a list of the first k items and call heapify. A more optimized (but still O(n log k)) algorithm is:

# create array of first `k` items
heap = heapify(array)
for remaining nums
    if (num > heap.peek())
        heap.replace(num)
return heap.pop()

You could also call heapify on the entire array, then pop the first n-k items, and then take the top:

heapify(nums)
for i = 0 to n-k
    heapq.heappop(nums)
return heapq.heappop(nums)

That's simpler. Not sure if it's faster than my previous suggestion, but it modifies the original array. The complexity is O(n) to build the heap, then O((n-k) log n) for the pops. So it's be O((n-k) log n). Worst case O(n log n).

2 of 6
16

heapify() actually takes linear time because the approach is different than calling heapq.push() N times.

heapq.push()/heapq.pop() takes log n time because it adjust all the nodes at a given hight/level.

when you pass an array in heapify() it makes sure that the left and right children of the node are already maintaining the heap property whether it is a min heap or max heap.

you can see this video: https://www.youtube.com/watch?v=HqPJF2L5h9U

https://www.youtube.com/watch?v=B7hVxCmfPtM

Hope this would help.