On this page
heapq.heapify
Up: heapq | Next: heapq.heapify_max
Rel: heapq.heapify_max, heapq.heappop, heapq.heappop_max, heapq.heappush
Ref: docs.python.org — heapq.heapify
Tags: #public #python3 #stdlib #heapq
Import
import heapq
from heapq import heapify
Signature & Overview
heapq.heapify(heap, /)
Transform list into a heap, in-place, in O(len(heap)) time.
Usage
import heapq
nums = [5, 1, 8, 3]
heapq.heapify(nums)
assert nums[0] == 1 # min-heap invariant: smallest element at index 0
Gotchas
- In-place $O(n)$ min-heap construction (returns
None): Notenumsis NOT fully sorted—onlynums[0]is guaranteed to be the minimum.
REPL Inspect
from heapq import heapify
help(heapify)
Linked from
- tuts/python3/stdlib_module/heapq
- tuts/python3/stdlib_module/heapq/heapify_max
- tuts/python3/stdlib_module/heapq/heappop
- tuts/python3/stdlib_module/heapq/heappop_max
- tuts/python3/stdlib_module/heapq/heappush
- tuts/python3/stdlib_module/heapq/heappush_max
- tuts/python3/stdlib_module/heapq/heappushpop
- tuts/python3/stdlib_module/heapq/heappushpop_max
- tuts/python3/stdlib_module/heapq/heapreplace
- tuts/python3/stdlib_module/heapq/heapreplace_max
- tuts/python3/stdlib_module/heapq/merge
- tuts/python3/stdlib_module/heapq/nlargest
- tuts/python3/stdlib_module/heapq/nsmallest
Related notes
- tuts/python3/stdlib_module/heapq/heapify_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappop_max Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappush Linked here; links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq/heappush_max links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappushpop links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heappushpop_max links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapreplace links to this page; shared tags: #heapq, #stdlib
- tuts/python3/stdlib_module/heapq/heapreplace_max links to this page; shared tags: #heapq, #stdlib