**Introduction**

Heaps are a set of concrete data structures implementations for use-cases. It is always required to retrieve the smallest, largest element (or any other sort of priority criteria relevant there). But one can contemplate and debate that it can efficiently do this by sorting all elements in ascending/descending order and retrieving the min/max elements required each time. Still, what happens when one needs to also insert multiple elements after removals/retrievals? For that, one would need to resort the elements to maintain that required order; one can now easily understand how inefficient that would be for solving use-cases such as keeping track of queues of long-running processes/tasks and maintaining an order of priority there, maintaining a set of most recent logs in a system and keeping track of changes etc. it can solve all types of such a wide range of challenges and various problems by using heaps and priority queues.

Let's understand these functionalities with helpful examples covered in the article.

Also See, __Intersection in Python____, ____Swapcase in Python__

**Operations using Heapq**

In __Python__, the heapq module offers various operations for initialisation, insertions and retrievals in the heap. The functions in heapq are all operations that can be performed on lists in Python directly and do not require a custom implementation of a class to be utilised. One can use or implement the following set of heap operations using the heapq module:

- heapify(list)
- heappush(heap, item)
- heappop(heap)
- heappushpop(heap, item)
- heapreplace(heap, item)
- nlargest(item, iterable, key=function)
- nsmallest(item, iterable, key=function)

**1. heapify**

The 'heapify' method is used on a list to perform a heapify operation on it (as the name suggests), which transforms all the elements in the original list that is passed into the function as an argument into a heap (by default, it is a min-heap implementation in Python).

```
# First importing the heapq Library
import heapq
# initialising list in python
l = [5, 1, 9, 6, 4, 2, 10]
# performing heapify on list ‘l’
d = 10
# heapified elements of list ‘l’
print( l )
```

**Output:**

`[1, 4, 2, 6, 5, 9, 10]`

Note: The heapify operation modifies all the elements of the list in place and creates a min-heap by default where the minimum element is always at the root

**2. heappop**

The ‘heappop’ method is used to retrieve and remove the minimum element from the heap by maintaining the heap property of the list both before and after the removal.

```
# First importing the heapq Library
import heapq
l = [1, 4, 2, 6, 5, 9, 10]
# Then printing the value of the popped item from the heap
print (heapq.heappop(l))
```

**Output:**

`1`

**3. heappush**

The 'heappush' takes two parameters viz. Heap to which the given element is to be inserted and the value of the element that it would insert.

```
# First importing the heapq Library
import heapq
l = [2, 4, 9, 6, 5, 10]
heapq.heappush(l, 3)
print ( l )
```

**Output:**

`[2, 4, 3, 6, 5, 10, 9]`

**4. heapreplace**

This method is a combined implementation of a ‘heappop’ followed by a ‘heappush’

```
# First importing the heapq Library
import heapq
l = [2, 4, 3, 6, 5, 10, 9]
popped_item = heapq.heapreplace(l, 8)
print(“Popped : “,popped_item)
print(“list after heapreplace: “,l)
```

**Output:**

```
Popped : 2
list after heapreplace: [3, 4, 8, 6, 5, 10, 9]
```

You can compile it with online python compiler.

**5. heappushpop**

The ‘heappushpop’ method is a combination of a ‘heappush’ followed by a ‘heappop’

```
# First importing the heapq Library
import heapq
l = [3, 4, 8, 6, 5, 10, 9]
popped_item = heapq.heapreplace(l, 4)
print(“Popped : “,popped_item)
print(“list after heappushpop: “,l)
```

**Output:**

```
Popped : 3
list after heappushpop: [4, 4, 8, 6, 5, 10, 9]
```

**6. nlargest and nsmallest**

These methods in the heapq module are used to retrieve a list of 'N' largest or 'N' smallest elements in a heap. This function takes the iterable as the first parameter, the number of elements to be retrieved as the second and a third optional parameter as a custom comparator function to filter and retrieve such elements internally.

```
# First importing the heapq Library
import heapq
l = [4, 4, 8, 6, 5, 10, 9]
print(“The 4 smallest items in the heap are : “,heapq.nsmallest(4, l))
print(“The 4 largest items in the heap are : “,heapq.nlargest(4, l))
```

**Output**

```
The 4 smallest items in a heap are : [4, 4, 5, 6]
The 4 largest items in a heap are : [10, 9, 8, 6]
```

Must Read __Python List Operations__