Skip to main content

bisect

bisect maintains a list in sorted order without sorting after each insertion. Built on binary search.

Source-of-record: Lib/bisect.py, Modules/_bisectmodule.c, bisect docs.

Locate

FunctionReturns
bisect_left(a, x, lo=0, hi=len(a), *, key=None)Index where x could be inserted to keep sorted; ties go to the left.
bisect_right(a, x, lo=0, hi=len(a), *, key=None)Same; ties go to the right.
bisect(...)Alias for bisect_right.

Insert

FunctionEffect
insort_left(a, x, lo=0, hi=len(a), *, key=None)Insert x keeping sorted; ties left.
insort_right(a, x, lo=0, hi=len(a), *, key=None)Insert keeping sorted; ties right.
insort(...)Alias for insort_right.

The key argument applies a one-arg key function similar to sorted(..., key=). The list must already be sorted by that key.

Membership and find

bisect doesn't ship find/contains; the recipe is:

i = bisect_left(a, x)
hit = i != len(a) and a[i] == x

Gopy status

AreaState
All locate / insert variantsComplete.
key argumentComplete.
C-fast pathsComplete via module/_bisect.

Reference

  • CPython 3.14: bisect.
  • Lib/bisect.py, Modules/_bisectmodule.c.
  • module/_bisect/. gopy port.