Bisection Functions

bisect.insort_left()

Insert x into a sorted mutable sequence before existing equal keys. The key function is applied to x for searching, but x itself is inserted. Search is logarithmic; list insertion is linear.

Formal Syntax & Structure

python syntaxpython
bisect.insort_left(a, x, lo=0, hi=None, *, key=None)

Parameters & Attributes

NameTypeDescription
asequenceSequence sorted by the same ordering as the search.
xobjectItem to insert.
lointInclusive lower search bound.
hiint or NoneExclusive upper search bound; None means length of sequence.
keycallable or NoneOptional key function used for comparison or grouping.

Return Value

None

No return value; the operation changes the receiver in place.

Working Example

Example Snippetpython
import bisecta=[1,2,2,4]bisect.insort_left(a,3)print(a)
Python Reference