[Python-Dev] Re: heapq API

Josiah Carlson jcarlson at uci.edu
Thu Jun 17 08:29:56 EDT 2004


> Raymond Hettinger wrote:
> > def heappushpop(heap, item):
> >     “Pushes the item onto the heap and then pops the smallest value”
> > 
> >     if not heap or item < heap[0]:
> >         return item
> >     return heapreplace(heap, item)

Scott David Daniels wrote:
> Better is:
>       if heap and heap[0] < item:
>           return heapreplace(heap, item)
>       return item

The or method short-circuits too.
>>> heap = []
>>> not heap or 7 < heap[0]
1
>>> 

 - Josiah




More information about the Python-Dev mailing list