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

Masklinn masklinn at masklinn.net
Wed Feb 8 23:00:39 CET 2012


The ``bisect`` stuff is pretty neat, although probably underused
(especially the insorts), but their usefulness is limited by the
requirement that the lists directly contain sortable items, as opposed
to ``sorted`` or ``list.sort``.

It's possible to "use" them by copy/pasting the (Python) functions
into the project/library code and adding either a custom key directly
or a key function, but while this can still yield an
order-of-magnitude speed gain over post-sorting sequences, it's
cumbersome and it loses the advantage of _bisect's accelerators.

Therefore, I believe it would be pretty neat to add an optional
``key=`` keyword (only?) argument, with the same semantics as in
``sorted``. It would make ``bisect`` much easier to use especially
in stead of append + sorted combinations. The key should work for
both insertion functions and bisection search ones.

Thoughts?



More information about the Python-ideas mailing list