title: How to iterate heapq without losing data?
body: <p>In Python 3 I'm using heapq like so:</p>
  
  <pre><code>import heapq
  
  heap = [3]
  heapq.heapify(heap)
  heapq.heappush(heap, 5)
  # Push more values...
  
  # I can iterate heap like so, but by the end it will be empty:
  while (heap):
      curr = heapq.heappop(heap)
      # Do whatever with curr
  </code></pre>
  
  <p><strong>Is there a way to iterate heapq such that I get the values in sorted order without mutating heapq/losing data?</strong></p>
  
  <p><strong>If not, how can I efficiently imitate the desired behavior?</strong></p>
  
  <p>The solution I came up with is to create a temporary heap, push to it as I pop from the original heap, and once I'm done iterating set the original heap equal to the temporary heap.</p>
  
  <p>Of course this is not very efficient, and changes the object which the original heap referenced.</p>
  
  <pre><code>temp = []
  heapq.heapify(temp)
  
  while(heap):
      curr = heapq.heappop(heap)
      heapq.heappush(temp, curr)
      # Do whatever with curr
  heap = temp
  </code></pre>