[Python-ideas] Optional key to `bisect`'s functions?

Guido van Rossum guido at python.org
Thu Feb 9 22:20:48 CET 2012

On Thu, Feb 9, 2012 at 12:34 PM, Raymond Hettinger <
raymond.hettinger at gmail.com> wrote:

> Bisect in particular had way too much focus on the algorithm.  The API is
> awkward and error-prone for many common use cases.
> I've tried to remedy that through documenting how to implement the common
> use cases:
> http://docs.python.org/py3k/library/bisect.html#searching-sorted-lists
> The issue is that the current API focuses on "insertion points" rather
> than on finding values.  Unfortunately, this API is very old, so the only
> way to fix it is to introduce a new class.
> If we introduced class around a sorted sequence, then we could make an
> reasonable API that corresponds to what people usually want to do with
> sorted sequences.
> Of course, that still leaves the issue with an O(n) insort.  As Daniel
> pointed-out, a list is not the correct underlying data structure if you
> want to do periodic insertions and deletions.

Maybe you're overanalyzing the problem? It seems what you want would
require a PEP and/or a reference implementation that is thoroughly tested
as a 3rd party package before it warrants inclusion into the stdlib. In the
mean time adding a key= option that echoes the API offered by list.sort()
and sorted() is a no-brainer.

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120209/1ee6e5ea/attachment.html>

More information about the Python-ideas mailing list