Heapify each sublist in a list of lists in python?
<p>I have a list of lists called opened which was declared like this:  </p>

<pre><code>opened = [[] for i in range(5)]
</code></pre>

<p>Now <code>opened = [[], [], [], [], []]</code><br>
How can I heapify each of the sublists using the <code>heapq.heapify()</code> function? i.e, opened[0], opened[1], opened[2], opened[3], opened[4] should be heapified.  </p>

<p>Thanks in advance.</p>

<p>Each of those sub-lists is a list, so just use <code>heapify()</code> directly on each of them:</p>

<pre><code>import heapq

for j in range(5):
    heapq.heapify(opened[j])
</code></pre>

<p>Of course, if you know that each of those sub-lists is empty, there is no need to do this: an empty list is already a heap. There are no extra variables or other storage to make them heaps, since a Python heap is just a list with a condition on the order of the elements.</p>

python data-structures heap