Python heapq.heappush() Method Last Updated : 11 Mar, 2025 Comments Improve Suggest changes Like Article Like Report The heapq.heappush() function in Python is used to push an element onto a heap while maintaining the heap property. This function is useful when dealing with priority queues or implementing efficient sorting algorithms.Example: Python import heapq # Create an empty list that will represent the heap h = [] # Use heappush to add elements to the heap heapq.heappush(h, 10) heapq.heappush(h, 5) heapq.heappush(h, 20) heapq.heappush(h, 15) # Print the heap after adding elements print(h) Output[5, 10, 20, 15] Explanation:The heappush() function ensures that the heap property is maintained, meaning the smallest element is always at the root (index 0).After pushing the values 10, 5, 20, and 15, the heap is automatically rearranged to maintain the heap property.Syntax of heappush() methodheapq.heappush(heap, item)Parametersheap: The list representing the heap (must be a valid heap structure).item: The element to be pushed onto the heap.Return ValueThis function does not return anything. It modifies the heap in-place by inserting the new element while maintaining the heap property.Examples of heappush() method1. Using heappush() to Insert Elements in a Min-Heap Python import heapq # Create an empty heap h = [] # Push elements onto the heap heapq.heappush(h, 5) heapq.heappush(h, 1) heapq.heappush(h, 8) heapq.heappush(h, 3) print("Min-Heap:", h) OutputMin-Heap: [1, 3, 8, 5] Explanation:The heappush() function is used to insert the values 5, 1, 8, and 3 into the heap.The heap automatically rearranges itself to maintain the min-heap property, where the smallest element is always at the root (index 0).2. Using heappush() in a Max-HeapSince heapq only supports min-heaps, we can simulate a max-heap by pushing negative values. Python import heapq h = [] # Push elements as negative values to simulate max-heap heapq.heappush(h, -5) heapq.heappush(h, -1) heapq.heappush(h, -8) heapq.heappush(h, -3) # Convert back to positive for correct order max_heap = [-x for x in h] print("Max-Heap:", max_heap) OutputMax-Heap: [8, 3, 5, 1] Explanation:Elements are pushed onto the heap as negative values (-5, -1, -8, -3) to simulate the behavior of a max-heap.The heappush() function inserts the negative values, maintaining the min-heap property on the negative values, which results in a simulated max-heap.After inserting all elements, the heap is converted back to positive values using list comprehension ([-x for x in h]) to display the correct max-heap order.3. Using heappush() in a Priority Queueheapq.heappush() is commonly used in priority queues, where elements are inserted based on their priority. Python import heapq # List of tuples (priority, task) pq= [] # Push elements (priority, task) heapq.heappush(pq, (2, "Task A")) heapq.heappush(pq, (1, "Task B")) heapq.heappush(pq, (3, "Task C")) print("Priority Queue:", pq) OutputPriority Queue: [(1, 'Task B'), (2, 'Task A'), (3, 'Task C')] Explanation:Elements are pushed onto the heap as tuples, where the first value is the priority (lower values indicate higher priority) and the second value is the task.The heappush() function ensures that the queue is sorted according to the priority, with the element having the lowest priority value at the root. Comment More infoAdvertise with us Next Article Python heapq.heappush() Method B brijkan3mz4 Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Heap Sort - Python Heapsort is a comparison-based sorting technique based on a Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element.Heap Sort AlgorithmFirst convert the array in 4 min read Binomial Heap Using Python Binomial Heap is an extension of Binary Heap that provides faster union or merge operations with other operations provided by Binary Heap. A Binomial Heap is a collection of Binomial Trees. The main application of Binary Heap is as implement a priority queue. To Know More about Binomial Heap Click 5 min read Sorting algorithm visualization : Heap Sort An algorithm like Heap sort can be understood easily by visualizing. In this article, a program that visualizes the Heap Sort Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach: Generate random array and fill the pygame window wi 4 min read Find Largest Item in a Tuple - Python We need to find the largest number or the top N largest numbers from a tuple.For Example:Input: (10,20,23,5,2,90) #tupleOutput: 90Explanation: 90 is the largest element from tupleThere are several efficient ways to achieve this:Using max()max() in Python is the most efficient way to find the largest 3 min read Enqueue in Queues in Python A queue is a basic data structure that follows the First-In-First-Out (FIFO) principle. At the back of the queue, elements are added (enqueued), and at the front, they are removed (dequeued). In this article, we will see the methods of Enqueuing (adding elements) in Python. Enqueue: The act of addin 2 min read Like