On this page
bisect.insort
Prev: bisect.bisect_right | Up: bisect | Next: bisect.insort_left
Rel: bisect.bisect, bisect.bisect_left, bisect.bisect_right, bisect.insort_left
Ref: docs.python.org — bisect.insort
Tags: #public #python3 #stdlib #bisect
Import
import bisect
from bisect import insort
Signature & Overview
bisect.insort(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, 3, 5]
bisect.insort(arr, 4)
assert arr == [1, 3, 4, 5]
Gotchas
- Search is $O(\log n)$, but
list.insert()is $O(n)$: Fine for small lists; for large dynamic sorted sets, useheapqorsortedcontainers.
REPL Inspect
from bisect import insort
help(insort)
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_left Linked here; links to this page; shared tags: #bisect, #stdlib
- tuts/python3/stdlib_module/bisect/insort_right 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