I'm using the binary search function mentioned here: When are bisect_left and bisect_right not equal?, but instead of returning False, I want to just skip values that aren't in the list e.
from bisect import bisect_left
def binsearch(l,e):
index = bisect_left(l,e)
if index == len(l) or l[index] != e:
return False
return index
l = [1, 2, 3, 6, 7, 8, 9]
e = [7, 9, 2, 4, 7]
index = []
for i in e:
index.append(binsearch(l,i))
print index # [4, 6, 1, False, 4]
I tried replacing return False with pass but am getting the index of where the value not in the list would be placed. Is there a way to simply pass a value if it is not in l and output [4, 6, 1, 4]?