Search code examples
pythonheappriority-queueheapq

What types of heap elements can be used with heapq module in python?


In the documentation, it mentions it could be tuples. But could it be lists? If yes, then is the priority decided by the first element of the list by default? Because in the Priority Queue Implementation Notes in the documentation, they have used list to illustrate it?


Solution

  • Python allows you to heapify any Python iterable (lists, tuple, strings, etc).

    So, yes lists and tuples can be used as elements instead of just integers but only as long as the iterable can support valid comparisons between its elements in the lexicographical order. Let's get our hands dirty.

    >>> a = (0, "hello")
    >>> b = (1, "word")
    >>> array = [a, b]
    >>> heapq.heapify(array)
    >>> heapq.heappop(array)
    (0, 'hello')
    

    All looks good, we were able to heapify a list of tuples where each tuple contains an int and a string. Let's look at another example:

    >>> a = (0, "hello")
    >>> b = ("word", 1)
    >>> array = [a, b]
    >>> heapq.heapify(array)
    >>> heapq.heapify(array)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: '<' not supported between instances of 'int' and 'str'
    

    As you can see, python interpreter starts complaining because it could not compare int & str.

    For the same reasons, you won't be able to heapify a list of dictionary (List[Dict]) but you would be able to heapfiy a list of int (List[int]) or even a list of list of int (List[List[int]]). Here is the proof:

    >>> a = {0:'hello', 1:'world'}
    >>> b = {0:'hola', 1:'mundo'}
    >>> array = [a, b]
    >>> heapq.heapify(array)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: '<' not supported between instances of 'dict' and 'dict'
    >>>
    >>>
    >>> a = [1,2,3,4]
    >>> b = [5,6,7]
    >>> array = [a, b]
    >>> heapq.heapify(array)
    >>> heapq.heappop(array)
    [1, 2, 3, 4]