-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bisect.insort should return the index #96571
Comments
The norm for Python is for mutating methods is to return
That doesn't make sense to me. Why wouldn't you just do the bisection first and then insert?
I'll send @grantjenks a note to find out more about what he was thinking. |
Thank you for your response. Yes, one can do replace insort with these two steps (like I already wrote, maybe I edited that line after you read it), but without measuring I think I can safely assume that doing this in python is much slower than the C implementation of insort. So the only argument left is "we always return None for mutating methods". edit: |
If |
Yes, but just as I already wrote twice, I assume that it still would be slower than the C implementation of insort. But does it even matter? If everything I write was correct, would this be enough to break the rule to always return None? |
What if there's code that relies on the Wouldn't it create (and at some point delete) a Python |
They both share the same underlying code, ins1(). Aside from calling overhead, the speed is identical.
Your mental model seems to be inaccurate. The Python call |
Okay, thank you very much for your responses. You convinced me. Thank you for your time and explanations. |
I would suggest, you implement a version of what you like as a third party library. Cython is a really convenient way to write C-extensions, if you need that. You can call your library 'better-bisect' if you want to. Look at 'more-itertools' for inspiration. |
@rhettinger wrote:
And indeed, that's what the code in
@pochmann raises two points:
That's a good point. I'm not sure it's worth a breaking change.
Also a good point. And in SortedContainers, maintaining the positional index is much more expensive which is why it's not done by default. Thanks @Serpens66 for asking here and to all the helpful replies. |
Feature or enhancement
https://docs.python.org/3/library/bisect.html#bisect.insort
bisect.insort (and of course also insort_left/right) should return the index they inserted the "x" in "a".
Pitch
Usecase:
One wants to insort something and also needs the index it was insorted.
Problem:
The current way would be to use "insort" (which finds the index and inserts it, but does not return the index) and afterwards use bisect_right to find the index again. So this is ~double of the work that is needed.
I assume the user could also instead call bisect_right and afterwards insert "x" in "a" themself and return the index, but I'm sure this also would be much slower than the C implementation of bisect.insort.
Solution:
Simply let "insort" return the index it already found. This should be backwards compatible (since it returns None currently) and not cost any extra time. So the usecase would be fullfilled in ~half of the time that is needed right now.
Disadvantage:
I see no harm in doing so, only advantage for everyone who needs this.
Projects I know which would profit:
grantjenks/python-sortedcontainers#195
edit::
I was a bit confused because the python source code has a python and a C version of bisect... But both versions lack the return of the index, so this request is valid for both.
The text was updated successfully, but these errors were encountered: