Mallika Bachan <mallika.bac...@gmail.com> added the comment:

Conceptually, yes, but the function does return an index, and values are
stored at these indices. The index it returns holds the leftmost occurrence
of the target (should the target exist)
>>> bisect.bisect_left([1,2,2,3],2)
1
If we insert at this index, it will push the current value right, so
conceptually, sure, it can help to think of the insertion point as being
just before the leftmost target value.

But while bisect_right does in fact return value i that "points just beyond
the rightmost x already there" ("just beyond" gets interpreted as "the next
one", because only whole indices are used), making the statement "i points
just before the leftmost x already there" for the return value of
bisect_left definitely appears incorrect.

On Mon, May 24, 2021 at 6:30 PM Raymond Hettinger <rep...@bugs.python.org>
wrote:

>
> Raymond Hettinger <raymond.hettin...@gmail.com> added the comment:
>
> I there is a misunderstanding here.  The bisect functions never point *to*
> a value.  Instead, they are documented to return "insertion points".  Those
> always occur just before or after a specific value:
>
> values:              10   20   30   30   30   40   50
> insertion points:   |   |    |    |    |    |    |    |
>                     0   1    2    3    4    5    6    7
> bisect_left(30) -------------^
> bisect_right(30) ---------------------------^
>
> As you can see, bisect_left() does in fact point JUST BEFORE the 30.
>
> Note this is also how slicing works.  Here's an example:
>
>     >>> from bisect import bisect_left, bisect_right
>     >>> s = [10, 20, 30, 30, 30, 40, 50]
>     >>> i = bisect_left(s, 30)
>     >>> j = bisect_right(s, 30)
>     >>> s[i : j]
>     [30, 30, 30]
>
> ----------
>
> _______________________________________
> Python tracker <rep...@bugs.python.org>
> <https://bugs.python.org/issue44227>
> _______________________________________
>

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44227>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to