I have no idea about python, but from an algorithmic point of view, maybe I can add something. In your example, your list is monotonously increasing (sorting). If this is always true for your list, then a little optimization may be to stop the repetition as soon as you reach a number greater than 4.
If your list always has a few digits below 4, this will be a great optimization, but if the number of elements before and after the target number is random, then this improvement is not reliable.
In this case, you may need to search the list by dividing it. Check if the middle element is larger than 4. If it is larger, discard the upper half, otherwise discard the lower half. Do the same in the new half-cut list. You need to deal with even and odd numbers and with the case when only 1 or 2 elements are left in the list segment. For a large list, this should significantly reduce the number of tests.
Mithon
source share