bisect_left and bisect_right
Find insertion points in a sorted list.
bisect_left and bisect_right is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Search Without the Boilerplate
Python's bisect module gives you a tested binary search for sorted lists. No hand-written loop means no off-by-one bugs to debug.
import bisectInsertion Points, Not Booleans
Instead of true or false, bisect returns an index where a value would be inserted to keep the list sorted. That index is the real power.
a = [1, 3, 3, 3, 7]bisect_left Leans Left
bisect_left returns the first position where the value could go. For duplicates it lands before all equal items, never after.
bisect.bisect_left(a, 3) # 1bisect_right Leans Right
bisect_right returns the position just past the last equal item. With duplicates it lands after every matching value.
bisect.bisect_right(a, 3) # 4Count Equal Elements
Subtract the two to count duplicates of a value in O(log n). right minus left gives exactly how many times it appears.
lo = bisect.bisect_left(a, 3)
hi = bisect.bisect_right(a, 3)
print(hi - lo) # 3Did the Value Exist?
To check membership, get i from bisect_left and confirm a[i] equals the target. Guard against i reaching the list length first.
i = bisect.bisect_left(a, x)
found = i < len(a) and a[i] == xFirst Element At Least X
bisect_left also finds the first element greater than or equal to x. That index points straight at your lower-bound answer.
i = bisect.bisect_left(a, x) # first >= xFirst Element Strictly Greater
Need the first element strictly greater than x? bisect_right gives that index directly, the upper-bound counterpart.
i = bisect.bisect_right(a, x) # first > xInsert and Stay Sorted
insort finds the spot and inserts in one call, keeping the list ordered. Handy when you build a sorted structure on the fly.
bisect.insort(a, 5) # a stays sortedSearch Inside a Window
Optional lo and hi arguments restrict the search to a slice. This avoids copying when you only care about a sub-range.
bisect.bisect_left(a, x, 2, 5)Keys via a Helper List
bisect compares whole elements, so to search by a field, build a parallel list of just those keys and bisect that instead.
keys = [p[0] for p in pairs]
i = bisect.bisect_left(keys, target)Quick Check
Reason about duplicates and insertion points.
Recap: Bisect Mastery
You can now find insertion points, count duplicates, and locate lower and upper bounds in log time. Reach for bisect before writing a loop. ✨
Frequently asked questions
Is the “bisect_left and bisect_right” lesson free?
Yes — the full text of “bisect_left and bisect_right” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “bisect_left and bisect_right”?
Find insertion points in a sorted list. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “bisect_left and bisect_right” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Classic Binary Search Without Bugs
- bisect_left and bisect_right
- First True: Predicate Binary Search
- Binary Search on the Answer