Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c8dd0c

Browse files
committed
Simplify heapreplace() -- there's no need for an explicit test for
empty heap, since heap[0] raises the appropriate IndexError already.
1 parent b286591 commit 3c8dd0c

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

Lib/heapq.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,10 @@ def heapreplace(heap, item):
150150
returned may be larger than item! That constrains reasonable uses of
151151
this routine.
152152
"""
153-
154-
if heap:
155-
returnitem = heap[0]
156-
heap[0] = item
157-
_siftup(heap, 0)
158-
return returnitem
159-
heap.pop() # raise IndexError
153+
returnitem = heap[0] # raises appropriate IndexError if heap is empty
154+
heap[0] = item
155+
_siftup(heap, 0)
156+
return returnitem
160157

161158
def heapify(x):
162159
"""Transform list into a heap, in-place, in O(len(heap)) time."""

0 commit comments

Comments
 (0)