Open In App

Python heapq.heapify() Method

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The heapq.heapify() function in Python is used to transform a regular list into a valid min-heap. A min-heap is a binary tree where the smallest element is always at the root. This method is highly useful when you need to create a heap structure from an unordered list and maintain the heap property efficiently.

Example: Converting a List into a Min-Heap

Python
import heapq

# Create a regular list
a = [3, 1, 5, 7, 9, 2]

# Convert the list into a heap
heapq.heapify(a)

print("Min-Heap:", a)

Output
Min-Heap: [1, 3, 2, 7, 9, 5]

Explanation:

  • The list [3, 1, 5, 7, 9, 2] is rearranged so that the smallest element (1) is at the root of the heap.
  • After the operation, the heap is [1, 3, 2, 7, 9, 5], where 1 is the smallest element.

Syntax of heapify() method

heapq.heapify(iterable)

Parameters

  • iterable: The iterable (usually a list) that you want to convert into a heap. This list will be rearranged in place to satisfy the heap property (for min-heaps, the smallest element will be at the root).

Return Value

The heapq.heapify() method does not return anything. It modifies the input list in place, ensuring that the list satisfies the heap property, where the smallest element is at the root.

How Does heapq.heapify() Work?

  • The heapq.heapify() function rearranges the elements in the list to make it a valid min-heap.
  • The heap property is maintained after this operation, so the smallest element will always be at index 0.
  • It runs in O(n) time complexity, where n is the number of elements in the list. This is more efficient than using heapq.heappush() repeatedly to insert elements, which would take O(n log n) time.

Examples of heapify() method

1. Using heapq.heapify() on a Custom List

Python
import heapq

# Create a custom list
a = [8, 4, 3, 9, 2, 5]

# Convert the list into a heap
heapq.heapify(a)

print("Heapified List:", a)

Output
Heapified List: [2, 4, 3, 9, 8, 5]

Explanation:

  • The list [8, 4, 3, 9, 2, 5] is rearranged so that the smallest element (2) is at the root of the heap.

2. Using heapq.heapify() to Implement a Priority Queue

Python
import heapq

# List of tasks with (priority, task)
a = [(2, "Task A"), (1, "Task B"), (3, "Task C")]

# Convert the list into a heap
heapq.heapify(a)

# Pop the task with the highest priority (lowest priority value)
priority, a = heapq.heappop(a)

print("Highest priority task:", a)

Output
Highest priority task: Task B

Explanation:

  • The list of tasks is converted into a heap, where tasks are processed based on their priority.
  • The task with the smallest priority value (highest priority) is popped first, in this case, Task B.

When to Use heapq.heapify()?

You can use heapq.heapify() when you need to efficiently create a heap from an unordered list. Some common use cases include:

  • Priority Queues: When tasks or elements need to be processed based on priority, and you want to quickly convert an unordered list into a priority queue.
  • Graph Algorithms: When using algorithms like Dijkstra's Shortest Path or A Search*, which often require the use of heaps.
  • Efficient Sorting: When performing heap sort, where the elements are repeatedly popped from the heap to get them in sorted order.
  • Merging Sorted Lists: When merging multiple sorted lists or processing data that can be efficiently handled by a heap structure.

Next Article
Practice Tags :

Similar Reads