From: Guido van Rossum Date: Wed, 7 Aug 2002 18:58:11 +0000 (+0000) Subject: Simplify heapreplace() -- there's no need for an explicit test for X-Git-Tag: v2.3c1~4629 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c8dd0c6e783b2a07017d0fdf7f60232603ed407;p=python Simplify heapreplace() -- there's no need for an explicit test for empty heap, since heap[0] raises the appropriate IndexError already. --- diff --git a/Lib/heapq.py b/Lib/heapq.py index 47326f3606..497043772d 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -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."""