0

I have a list of lists called opened which was declared like this:

opened = [[] for i in range(5)]

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

Thanks in advance.

Nihal Rp
  • 484
  • 6
  • 15

1 Answers1

3

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

import heapq

for j in range(5):
    heapq.heapify(opened[j])

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.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50