bisect
β’ 134 words β’ 1 min β’ updated
Thank you Heitor for letting me know about this module!
python
>>> import bisect
>>> a = [1, 2, 2, 3, 3, 4]
# l r
>>> assert bisect.bisect_left(a, 2) == 1
>>> assert bisect.bisect_right(a, 2) == 3
>>> a = [1, 2, 2, 3, 3, 4]
# lr
>>> assert bisect.bisect_left(a, 2.5) == 3
>>> assert bisect.bisect_right(a, 2.5) == 3bisect.bisect_left returns the leftmost index of a given number if it is
present in the list, or the index of the lowest number above it if it is not.
bisect.bisect_right returns the index after the one of the rightmost number if
it is present in the list, or the index of the lowest number above it if it is
not.
bisect.bisect is an alias for bisect.bisect_right.
See also this post by Martin Heinz.