org.pycforge.heapq · Author: PyCForge
License: GPL-3.0-only · Created: 2026-11-20
Runtime conversion · Direct declared calls
Using this import · Functions · Conversion contract · Worked examples
heapify · heappop · heappush · heappushpop · heapreplace
Build a min-heap and maintain it as items arrive or leave.
Use *import heapq as q. Store (priority, sequence, payload) tuples with a unique sequence number to break equal priorities before comparing payloads.
Use a list with mutually orderable keys. For equal priorities, a unique sequence number keeps payloads out of the comparison.
Five positional min-heap operations.
Use *import heapq in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.
Pass a Runtime list by position. heapify establishes the min-heap order. The other operations expect an existing heap. Items use Runtime less-than comparisons. Equal priorities can use a sequence number before the payload. A comparison error preserves list changes already made. Insertion stores the original value, and returning an item preserves its identity. Runtime work and allocation limits apply. User-defined ordering, max-heaps, merge, nlargest, nsmallest and Typed conversion are outside this profile.
Rearrange the list into a min-heap in place and return None.
Normalized semantic slots: heap. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Mutable list containing the heap.
Returns: None.
View example: queue-operations
Remove and return the smallest item. An empty heap raises IndexError.
Normalized semantic slots: heap. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Mutable list containing the heap.
Returns: The item removed from index zero.
View example: queue-operations
Insert the item into the heap and return None.
Normalized semantic slots: heap, item. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Mutable list containing the heap.
Argument 2: The original item to consider for insertion.
Returns: None.
View example: queue-operations
Return the smaller item and keep the larger. An empty heap returns the incoming item.
Normalized semantic slots: heap, item. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Mutable list containing the heap.
Argument 2: The original item to consider for insertion.
Returns: The old root when it is strictly less than the incoming item, otherwise the incoming item.
View example: queue-operations
Return the old root and insert the incoming item. An empty heap raises IndexError.
Normalized semantic slots: heap, item. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Mutable list containing the heap.
Argument 2: The original item to consider for insertion.
Returns: The old root, even when the incoming item has a lower priority.
View example: queue-operations
Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.
*import heapq as q
def main():
jobs = [5, 1, 3]
print(q.heapify(jobs), jobs)
print(q.heappush(jobs, 2), jobs)
print(q.heappop(jobs), jobs)
print(q.heappushpop(jobs, 4), jobs)
print(q.heapreplace(jobs, 1), jobs)
return 0
Expected standard output:
None [1, 5, 3] None [1, 2, 3, 5] 1 [2, 5, 3] 2 [3, 5, 4] 3 [1, 5, 4]
Expected exit code: 0