Python bisect finds insertion points in sorted lists; insort inserts while keeping order
Answer: Use bisect_left(a, x) or bisect_right(a, x) to locate the insertion point for x in a sorted list, and insort(a, x) to insert x in sorted order. The functions are designed to locate an insertion point rather than search for a specific value, and they only call __lt__().
Examples
from bisect import bisect, bisect_left, insort
def grade(score):
i = bisect([60, 70, 80, 90], score)
return "FDCBA"[i]
[grade(s) for s in [33, 99, 77, 70, 89, 90, 100]]
# ['F', 'A', 'C', 'C', 'B', 'A', 'A']
def index(a, x):
'Locate the leftmost value exactly equal to x'
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError
left vs right
bisect_left: if x is already present, the insertion point is before (to the left of) any existing entries.bisect_right(aliasbisect): the insertion point comes after (to the right of) existing entries of x.
The key parameter
The key parameter, added in version 3.10, extracts a comparison key from each element. To support searching complex records, the key function is not applied to the x value; for insort, it is applied to x for the search step only.
Performance and safety
- The O(log n) search is dominated by the slow O(n) insertion step, so
insort()functions are O(n). - Use bisection for searching ranges of values; dictionaries are more performant for locating specific values.
- The functions are not thread-safe: concurrent
insort_left()from multiple threads may leave the list unsorted.
See also
Claims
- id: C1 claim: The bisect functions only call __lt__(). source: S1 evidence: "the functions only call the __lt__() method"
- id: C2 claim: Dictionaries are faster for locating specific values. source: S1 evidence: "For locating specific values, dictionaries are more performant."
- id: C3 claim: The bisect functions are not thread-safe. source: S1 evidence: "The functions in this module are not thread-safe."
- id: C4 claim: insort keeps the list sorted assuming it is already sorted. source: S2 evidence: "Insert item x in list a, and keep it sorted assuming a is sorted."
Sources
- [S1] bisect — Array bisection algorithm. Python docs. https://docs.python.org/3/library/bisect.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/bisect.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/bisect.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, bisect — Array bisection algorithm; python/cpython (GitHub), Lib/bisect.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, bisect — Array bisection algorithm; python/cpython (GitHub), Lib/bisect.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python bisect finds insertion points in sorted lists; insort inserts while keeping order." iinventedlight Library, iil:note:375rscv1, version of 2026-11-18. https://iinventedlight.com/library/notes/python-bisect/. License: CC-BY-4.0.
@misc{iil_note_375rscv1,
author = {{iinventedlight}},
title = {{Python bisect finds insertion points in sorted lists; insort inserts while keeping order}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-bisect/},
note = {Permanent ID iil:note:375rscv1. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-bisect.md.