I am trying to use Bisect to find where I should insert a new element.
The list to search is a list of lists. The inner list has two elements and I want to compare using the first element.
In the bisect function documentation, it says that the key function is supposed to take the element and get the value to compare. In this case, I am getting the first element of the list.
i = bisect.bisect([[1,2], [2,3], [3,4]], 4, lambda x: x[0])
I got the following error:
TypeError: 'function' object cannot be interpreted as an integer
I also tried to warp the item with a list but the problem persists
i = bisect.bisect([[1,2], [2,3], [3,4]], [4,5] , lambda x: x[0])
If I tried to remove the key function, it works if I warp the value by a list:
i = bisect.bisect([[1,2], [2,3], [3,4]], [4])
print(i)
> 3
However, I am still not sure why the key function is not working.
Any suggestions? Thanks in advance!