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,
As the others have pointed out, bisect_left and bisect_right return different results when the element being looked up is present in the list.
It turns out that bisect_left is more useful at hand, since it returns the exact index of the element being looked up if it is present in the list.
>>> import bisect
>>> bisect.bisect_left([1,2,3,4,5], 2)
1
Example of binary_search that uses bisect_left:
from bisect import bisect_left
def binsearch(l,e):
'''
Looks up element e in a sorted list l and returns False if not found.
'''
index = bisect_left(l,e)
if index ==len(l) or l[index] != e:
return False
return index
There will be a small change in the above code, if you want to use bisect_right instead of bisect_left and get the same result.