Heapify Function Of Heapq Module In Python

Function Name:





targetList - A Python list from which the min heap needs to be created.

Return Value:

None. The list is made a min heap in-place.


  • The method heapify() of heapq module in Python, takes a Python list as parameter and converts the list into a min heap.
  • A minheap is a binary tree that always satisfies the following conditions:
    • The root node holds the smallest of the elements
    • The subtrees below the root has all the elements greater than or equal to the element at their root. (i.e., The parent elements are less than the children)
    • When a new element is added, the “heap property” (i.e., the above two steps) will be applied.
  • The function heapify() works in-place.



# Example Python program that makes a Python list into a min heap

import heapq


# Create a Python list

data        = [4, 3, 9, 11, 7, 6, 14]


# Make a minheap


print("Min heap constructed from a Python list is:  %s:"%data)


The list is in the min heap form.

Min heap constructed from a Python list is: [3, 4, 6, 11, 7, 9, 14]


Min Heap created through heapq.heapify() in Python

Copyright 2023 © pythontic.com