For a sorted list li, returns the closest item to value

def best_fit(li, value):
    """For a sorted list li, returns the closest item to value"""

    index = min(bisect_left(li, value), len(li) - 1)

    if index in (0, len(li)):
        return index

    if li[index] - value < value - li[index-1]:
        return index
    else:
        return index-1