heapq "key" arguments

Raymond Hettinger python at rcn.com
Mon Aug 3 18:55:06 CEST 2009


[Duncan Booth]
> 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]

Yes.   The code for nsmallest and nlargest preserves stability
so that the equivalence is maintained.


Raymond




More information about the Python-list mailing list