On this page
bisect.insort_left
Prev: bisect.insort | Up: bisect | Next: bisect.insort_right
Rel: bisect.bisect, bisect.bisect_left, bisect.bisect_right, bisect.insort
Ref: docs.python.org — bisect.insort_left
Tags: #public #python3 #stdlib #bisect
Import
import bisect
from bisect import insort_left
Signature & Overview
bisect.insort_left(a, x, lo=0, hi=None, *, key=None)
Insert item x in list a, and keep it sorted assuming a is sorted.
Usage
import bisect
arr = [1, 2, 3]
bisect.insort_left(arr, 2)
assert arr == [1, 2, 2, 3]
Gotchas
- Inserts before existing equal items: Supports
key=in Python 3.10+ (notekeyis called on every probe, not cached).
REPL Inspect
from bisect import insort_left
help(insort_left)
Linked from
Related notes
- tuts/python3/stdlib_module/bisect/bisect Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect/bisect_left Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect/bisect_right Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect/insort Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect/insort_right Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib