---
title: "bisect"
url: https://perrotta.dev/2025/11/bisect/
last_updated: 2026-01-05
---


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) == 3
```

`bisect.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](https://martinheinz.dev/blog/106) post by Martin Heinz.

