{
    "mode": "pydoc",
    "parameter": "_heapq",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/pydoc/_heapq/json",
    "generated": "2026-06-02T15:05:43Z",
    "sections": {
        "NAME": {
            "content": "heapq - Heap queue algorithm (a.k.a. priority queue).\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for\nall k, counting elements from 0.  For the sake of comparison,\nnon-existing elements are considered to be infinite.  The interesting\nproperty of a heap is that a[0] is always its smallest element.\n\nUsage:\n\nheap = []            # creates an empty heap",
            "subsections": [
                {
                    "name": "heappush",
                    "content": "item = heappop(heap) # pops the smallest item from the heap\nitem = heap[0]       # smallest item on the heap without popping it"
                },
                {
                    "name": "heapify",
                    "content": "item = heapreplace(heap, item) # pops and returns smallest item, and adds\n# new item; the heap size is unchanged\n\nOur API differs from textbook heap algorithms as follows:\n\n- We use 0-based indexing.  This makes the relationship between the\nindex for a node and the indexes for its children slightly less\nobvious, but is more suitable since Python uses 0-based indexing.\n\n- Our heappop() method returns the smallest item, not the largest.\n\nThese two make it possible to view the heap as a regular Python list\nwithout surprises: heap[0] is the smallest item, and heap.sort()\nmaintains the heap invariant!\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "heapify",
                    "content": "Transform list into a heap, in-place, in O(len(heap)) time.\n"
                },
                {
                    "name": "heappop",
                    "content": "Pop the smallest item off the heap, maintaining the heap invariant.\n"
                },
                {
                    "name": "heappush",
                    "content": "Push item onto heap, maintaining the heap invariant.\n"
                },
                {
                    "name": "heappushpop",
                    "content": "Push item on the heap, then pop and return the smallest item from the heap.\n\nThe combined action runs more efficiently than heappush() followed by\na separate call to heappop().\n"
                },
                {
                    "name": "heapreplace",
                    "content": "Pop and return the current smallest value, and add the new item.\n\nThis is more efficient than heappop() followed by heappush(), and can be\nmore appropriate when using a fixed-size heap.  Note that the value\nreturned may be larger than item!  That constrains reasonable uses of\nthis routine unless written as part of a conditional replacement:\n\nif item > heap[0]:\nitem = heapreplace(heap, item)\n"
                }
            ]
        },
        "DATA": {
            "content": "about = 'Heap queues\\n\\n[explanation by François Pinard]\\n\\nH... t...\n",
            "subsections": []
        },
        "FILE": {
            "content": "(built-in)\n\n",
            "subsections": []
        }
    },
    "summary": "heapq - Heap queue algorithm (a.k.a. priority queue).",
    "flags": [],
    "examples": [],
    "see_also": []
}