]> granicus.if.org Git - python/commitdiff
Simplify heapreplace() -- there's no need for an explicit test for
authorGuido van Rossum <guido@python.org>
Wed, 7 Aug 2002 18:58:11 +0000 (18:58 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 7 Aug 2002 18:58:11 +0000 (18:58 +0000)
empty heap, since heap[0] raises the appropriate IndexError already.

Lib/heapq.py

index 47326f360680e3b4fce22255baddec77088fc6a4..497043772d835853609f636c872f9df267b550d8 100644 (file)
@@ -150,13 +150,10 @@ def heapreplace(heap, item):
     returned may be larger than item!  That constrains reasonable uses of
     this routine.
     """
-
-    if heap:
-        returnitem = heap[0]
-        heap[0] = item
-        _siftup(heap, 0)
-        return returnitem
-    heap.pop()  # raise IndexError
+    returnitem = heap[0]    # raises appropriate IndexError if heap is empty
+    heap[0] = item
+    _siftup(heap, 0)
+    return returnitem
 
 def heapify(x):
     """Transform list into a heap, in-place, in O(len(heap)) time."""