python-sortedcontainers icon indicating copy to clipboard operation
python-sortedcontainers copied to clipboard

Feature Request: Add `key` parameter to bisect_left and bisect_right

Open pmadhikar opened this issue 2 years ago • 1 comments

Python 3.10 introduced the key parameter to their bisect functions. See for example https://docs.python.org/3/library/bisect.html#bisect.bisect_left

This allows one to bisect lists with values that cannot otherwise be compared directly.

It would be really awesome if the likes of SortedList.bisect_left also accepted this argument passed it down to the standard library bisect_* calls. For example here: https://github.com/grantjenks/python-sortedcontainers/blob/dff7ef79a21b3f3ceb6a19868f302f0a680aa243/sortedcontainers/sortedlist.py#L1166

It is only supported in python 3.10 though...

pmadhikar avatar Mar 07 '22 12:03 pmadhikar

SortedKeyList already provides bisect_key_left and bisect_key_right https://github.com/grantjenks/python-sortedcontainers/blob/dff7ef79a21b3f3ceb6a19868f302f0a680aa243/sortedcontainers/sortedlist.py#L2282

Ordering elements by key requires additional index structures internally. Specifying different key functions would require building a new sorted list. I’m not sure how the new API in Python 3.10 would benefit the project.

grantjenks avatar Mar 07 '22 15:03 grantjenks