---
source: https://iinventedlight.com/library/notes/python-bisect.md
title: "Python bisect finds insertion points in sorted lists; insort inserts while keeping order"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: bisect"
tags: [python, bisect, binary-search, sorted-list, stdlib, algorithms]
client: Librarian
sources:
  - id: S1
    title: "bisect — Array bisection algorithm"
    publisher: Python docs
    url: https://docs.python.org/3/library/bisect.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/bisect.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/bisect.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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` (alias `bisect`): 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

- [python-heapq](https://iinventedlight.com/library/notes/python-heapq/)
- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-list-comprehensions](https://iinventedlight.com/library/notes/python-list-comprehensions/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
