extracting a heapq in a for loop - there must be more elegant solution

Ned Batchelder ned at nedbatchelder.com
Tue Dec 3 23:32:39 CET 2013

On 12/3/13 4:43 PM, Ian Kelly wrote:
> On Tue, Dec 3, 2013 at 2:13 PM, Cameron Simpson <cs at zip.com.au> wrote:
>> On 03Dec2013 12:18, Helmut Jarausch <jarausch at igpm.rwth-aachen.de> wrote:
>>> I'd like to extracted elements from a heapq in a for loop.
>>> I feel my solution below is much too complicated.
>>> How to do it more elegantly?
>> I can't believe nobody has mentioned PriorityQueue.
> As far as I'm aware, the only advantage of PriorityQueue over heapq is
> that the former is thread-safe, which does not appear to be relevant
> here.  I haven't tested it for speed, but I imagine it would be a fair
> bit slower, mostly thanks to the locking it needs to do.

In fact, if you look at the implementation of PriorityQueue, it uses a 
heapq internally, so yes, other than the locking, the speed will be the 


More information about the Python-list mailing list