[Python-ideas] Sorted lists

Paul Moore p.f.moore at gmail.com
Mon Apr 8 05:34:19 EDT 2019


On Mon, 8 Apr 2019 at 10:10, Steven D'Aprano <steve at pearwood.info> wrote:

> Possibly the maintainer of bisect may decide that its not worth the
> change. But for the statistics module, I would certainly change the
> implementation of median() to look something vaguely like this:
>
>     # was
>     data = sorted(data)  # may be expensive if data is large
>
>     # may become
>     if not (isinstance(data, list) and data.__issorted__):
>         data = sorted(data)

So just to be clear, this would be a change in behaviour - for a list
that is currently sorted on a key that is *not* "numerically
ascending", the code will no longer re-sort, and so will give the
wrong answer? (Maybe the code is able to deal with either ascending or
descending orders, I don't know about that, but even so, that just
makes the failure rarer, not non-existent).

I'm not saying that this is forbidden, just want to be clear if that's
what you mean (because my difficulty with the proposed attribute is
that it seems unreliable enough that I can't imagine a case where I'd
feel comfortable using it myself).

Paul

PS I thought timsort was highly efficient given already sorted data?
Whether it's OK to rely on that I don't know, but did you take that
into account?


More information about the Python-ideas mailing list