0PricingLogin
Competitive Programming Academy · Lesson

bisect_left and bisect_right

Find insertion points in a sorted list.

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 bisect

Insertion 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]

All lessons in this course

  1. Classic Binary Search Without Bugs
  2. bisect_left and bisect_right
  3. First True: Predicate Binary Search
  4. Binary Search on the Answer
← Back to Competitive Programming Academy