Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4k points)

In my understanding, bisect_left and bisect_right are two different ways of doing the same thing: bisection, one coming from the left and the other coming from the right. Thus, it follows that they have the same result. Under what circumstances are these two not equal, i.e. when will they return different results, assuming the list and the value that is being searched are the same?

1 Answer

0 votes
by (8.7k points)

These are used to ensure that the list in python remains sorted after insertion.

 bisect_left():

It locates the insertion point for an element of the list to maintain sorted order. If it is already present in the list, the insertion point will be before (to the left of) any existing entries.

bisect right(): 

It is similar to bisect left() but provides the insertion point to the right of the existing entries.

They are not equal when the element that is to be inserted is in the list.

Browse Categories

...