Heapq

9 tutorials Updated 8 Jan 2020

  1. 1 The heapify function of heapq module in Python The heapify() function provided by the Python module heapq creates a min heap from a Python list. The list is modified in-place as required to create a min heap. 19 Dec 2019
  2. 2 heappop function of heapq module In Python The heappop() function of the heapq module in Python removes and returns the smallest element from the min heap. In a min heap the smallest element is at the root. The Python example constructs a min heap using heapq.heapify() and removes the smallest element twice. 24 Dec 2019
  3. 3 heappush function of heapq module in Python The heappush() function from the heapq module of Python adds an element into an existing heap while maintaining the heap property. The Python example creates a heap, adds elements to it and prints the heap. 24 Dec 2019
  4. 4 heappushpop function of heapq module in python The heappushpop() function from the Python heapq module first inserts an item into the min heap and returns the root node(i.e., the element with the lowest value) of the min heap after removing it. 8 Jan 2020
  5. 5 heapreplace function of heapq module in python The heapreplace function of the heapq module in Python pops the root node(i.e, the smallest element of the heap) and adds a new item while maintaining the heap property. 7 Jan 2020
  6. 6 The heapq module in Python The Python module heapq provides functions for creating a min heap and manipulating it by adding, removing elements to it. It also has functions for finding the specified number of smallest or largest elements in a min-heap. 3 Jan 2020
  7. 7 merge function of heapq module in python The merge function combines multiple Python sequences which are already in sorted order into a single sequence. The Python example merges a list, a tuple and a set into a single sorted sequence. 3 Jan 2020
  8. 8 nlargest function of heapq module in Python The function nlargest() from the Python module heapq returns the specified number of largest elements from a Python iterable. The Python examples sort elements of simple types like integer and objects of custom classes to print the output in the console. 27 Dec 2019
  9. 9 nsmallest function of heapq module In Python The nsmallest function of the heapq module in Python returns the specified number of smallest elements from a Python iterable. The nsmallest function also accepts a function as parameter which returns a comparison key to be used in sorting. 27 Dec 2019