heapq "key" arguments

Duncan Booth duncan.booth at invalid.invalid
Mon Aug 3 08:34:28 EDT 2009


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> wrote:

> Ok, it's not strictly the same, but usually it doesn't hurt. The heaqp
>  module doesn't promise anything about equal elements: it may keep the
>  original order, rearrange them at will, reverse them, whatever.

The documentation doesn't say anything directly about stability, but the 
implementation is actually stable. You can probably assume it must be at 
least for nlargest and nsmallest otherwise the stated equivalence wouldn't 
hold:

e.g. nsmallest documentation says:

    	Equivalent to: sorted(iterable, key=key)[:n]




-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list