def insort(col, element, get=lambda x: x):
    if not col:
        # If collection is empty go ahead and insert at the first position
        col.insert(0, element)
        return col

    lo, hi = 0, len(col)

    # Binary search for the correct position
    while lo < hi:
        mid = int((hi + lo) / 2)
        if get(col[mid]) > get(element):
            hi = mid
        else:
            lo = mid + 1

    col.insert(lo, element)

    return col
Python's bisect does not allow for a get/key
dataset/ETHPy150Open CenterForOpenScience/osf.io/website/files/utils.py/insort