bisect.insort_left() vs bisect.insort_right()
bisect.insort_left() and bisect.insort_right() are both Python Bisect Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. bisect.insort_left(): Inserts x into sorted list a at the leftmost position. Its syntax is bisect.insort_left(a, x, lo=0, hi=len(a)), and it returns none bisect.insort_right(): Inserts x into sorted list a at the rightmost position. Its syntax is bisect.insort_right(a, x, lo=0, hi=len(a)), and it returns none Example of bisect.insort_left(): a = [1, 3, 5]
bisect.insort_left(a, 4)
# a = [1, 3, 4, 5]. Example of bisect.insort_right(): a = [1, 3, 3, 5]
bisect.insort_right(a, 3)
# a = [1, 3, 3, 3, 5]. Read the full references for bisect.insort_left() and bisect.insort_right() to see parameters, edge cases, and example code side by side. Other Bisect Module APIs worth comparing: bisect.bisect_left(), bisect.bisect_right(). Both belong to the Bisect Module category in the Python Reference documentation.