def get_index_after_weight_limit(items_by_weight, weight_limit):

    """Given a list of (item_index, [any other fields,] item) tuples sorted by item weight, find the positional index that first exceeds the passed weight limit"""

    # find the item that first exceeds the weight limit; binary search code is based on bisect.bisect_right from standard Python library, but adapted to weight check
    lowest = 0
    highest = len(items_by_weight)
    while lowest < highest:
        middle = (lowest + highest) // 2
        if items_by_weight[middle][-1].weight >= weight_limit:
            highest = middle
        else:
            lowest = middle + 1
    return lowest