Heap operations:
1. heapify (iterable) : — this function is used to convert an iterable to a heap data structure. those. in heap order.
2. heappush (heap, ele) : — This function is used to insert an element mentioned in its arguments into the heap. Order is adjusted so that heap structure is preserved .
3. heappop (heap) : — this function is used to remove and return the smallest element from the heap. The order is adjusted so that heap structure is preserved .

Output:
The created heap is: [1, 3, 9, 7 , 5] The modified heap after push is: [1, 3, 4, 7, 5, 9] The popped and smallest element is: 1
4. heappushpop (heap, ele) : — This function combines the functionality of the push and pop operations in a single statement, increasing efficiency. Heap order is preserved after this operation.
5. heapreplace (heap, ele) : — This function also inserts and inserts an element in one statement, but it is different from the function above. When doing this, the element is fetched first, and then the — push.ie, a value greater than push may be returned.

Output:
The popped item using heappushpop ( ) is: 2 The popped item using heapreplace () is: 3
6. nlargest (k, iterable, key = fun) : — This function is used to return the k largest elements from the specified iterable and match the key, if mentioned.
7. nsmallest (k, iterable, key = fun) : — This function is used to return the k smallest elements from the specified iterable and match the key, if mentioned.

Output:
The 3 largest numbers in list are: [10, 9, 8] The 3 smallest numbers in list are: [1, 3 , 4]
This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.