heapq iteration?

Etienne Posthumus etienne at epoz.org
Wed Mar 17 10:35:28 EST 2004


On Wed, 17 Mar 2004, Eric @ Zomething wrote:

> I'm missing something critical about how heapq works.  I assumed I could iterate through the heap, but I get partial iteration:

Do this in stead:

l = [56, 208, 89, 413, 273, 199, 73, 21, 89, 13, 27, 199, 273, 413, 11, 22, 56, 2, 208]
heapq.heapify(l)
while l:
   m = heapq.heappop(l)
   print m, '\t', l

It is generally not a good idea to modify a list while you are iterating
over it. Remember that heappop actually modifies the list.

---

EP






More information about the Python-list mailing list