org.pycforge.bisect · Author: PyCForge
License: GPL-3.0-only · Created: 2026-11-20
Runtime conversion · Direct declared calls
Using this import · Functions · Conversion contract · Worked examples
bisect · bisect_left · bisect_right · insort · insort_left · insort_right
Find the position where a value belongs in a sorted sequence or insert it into a list.
Use *import bisect as b. Choose the left operation to precede equal keys or the right operation to follow them. The short names use the right-hand rule.
Use a sorted built-in sequence for a search and a list for insertion. Comparisons and key functions follow the Runtime types in the conversion contract.
Sorted search and list insertion with optional keys.
Declare *import bisect in the module preamble and call a member directly in Runtime mode. Supply key by keyword. A converted function can serve as the key.
Search an ascending list, tuple, range, str, bytes or bytearray within [lo, hi). Insertion requires a list and stores the original item by reference. lo accepts a nonnegative int64 or bool. Omitted hi, None and -1 use the current length. Other integer bounds are used as supplied. Probing beyond the sequence raises IndexError. There is no sortedness check. Values must support Runtime less-than comparison. Pass key by keyword. A search calls it on each probed element. An insertion also calls it once on the new item before searching. Callback exceptions propagate and callback side effects persist. The list is updated only after the search and insertion checks succeed. Each search takes logarithmically many probes. Unicode indexing and comparisons can add work. List insertion moves the following elements. Runtime work and allocation budgets apply. Custom sequence, comparison and __index__ methods, expanded arguments, first-class import members and Typed conversion are outside this profile.
Return the insertion index after existing equal keys.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: An integer index.
Return the insertion index before existing equal keys.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: An integer index.
Return the insertion index after existing equal keys.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: An integer index.
Insert the original item after existing equal keys and return None.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: None.
Insert the original item before existing equal keys and return None.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: None.
Insert the original item after existing equal keys and return None.
Normalized semantic slots: a, x, lo, hi, key. These slots are supplied by the engine; the signature above defines source arguments and defaults.
Argument 1: Ascending sequence to search, or a mutable list for insertion.
Argument 2: Search key, or the original item to insert.
Argument 3: Inclusive start index. The default is zero.
Argument 4: Exclusive stop index. The default uses the sequence length.
Argument 5: Optional one-argument Runtime callable, supplied by keyword.
Returns: None.
Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.
*import bisect as b
def priority(job):
return job[0]
def main():
queue = [(2, "first")]
b.insort_left(queue, (2, "early"), key=priority)
b.insort_right(queue, (2, "late"), key=priority)
b.insort(queue, (1, "urgent"), key=priority)
print(queue)
print(b.bisect_left(queue, 2, key=priority))
return 0
Expected standard output:
[(1, 'urgent'), (2, 'early'), (2, 'first'), (2, 'late')] 1
Expected exit code: 0
*import bisect as b
def main():
cutoffs = (10, 20, 20, 40)
print(b.bisect_left(cutoffs, 20), b.bisect_right(cutoffs, 20))
print(b.bisect(cutoffs, 20, lo=0, hi=2))
return 0
Expected standard output:
1 3 2
Expected exit code: 0