Python bisect Module
Example
Insert while keeping a list sorted:
import bisect
a = [1, 3, 4]
bisect.insort(a, 2)
print(a)
Try it Yourself »
Definition and Usage
The bisect module helps maintain a list in sorted order without having to sort after each insertion.
Use it for binary search and order-preserving insertions with functions like bisect and insort.
Members
| Member | Description |
|---|---|
| bisect() | Alias of bisect_right(). |
| bisect_left() | Locate insertion point for x to maintain sorted order (leftmost). |
| bisect_right() | Locate insertion point for x to maintain sorted order (rightmost). |
| insort() | Alias of insort_right(). |
| insort_left() | Insert x in a sorted list a, and keep it sorted (leftmost). |
| insort_right() | Insert x in a sorted list a, and keep it sorted (rightmost). |