def _binary_insert(lst, elem, key, lo=0, hi=None):
        if lo < 0:
            raise ValueError("lo must be a non-negative number")
        if hi is None:
            hi = len(lst)
        while lo < hi:
            mid = (lo + hi) // 2
            if key(lst[mid]) < key(elem):
                lo = mid + 1
            else:
                hi = mid
        lst.insert(lo, elem)