Bisect Cheat Sheet
Python's bisect module implements binary search on sorted arrays.
1. bisect_left
Finds the insertion point for x to maintain sorted order.
If x is already present, it returns the index of the first occurrence (the leftmost position).
nums = [10, 20, 20, 20, 30]
idx = bisect.bisect_left(nums, 20)
# Returns 1 (The first '20')
idx = bisect.bisect_left(nums, 25)
# Returns 4 (Insert before '30')
2. bisect_right (or bisect)
Finds the insertion point for x to maintain sorted order.
If x is already present, it returns the index just after the last occurrence (the rightmost position).
nums = [10, 20, 20, 20, 30]
idx = bisect.bisect_right(nums, 20)
# Returns 4 (Insert after the last '20')
3. Actually Inserting (insort)
If you want to search and insert in one step. Note: Inserting into a list is \(O(N)\) time!
Advanced: Using a key function (Python 3.10+)
If your list contains objects or tuples, you can provide a key function.