bisect_left and bisect_right
Binary search on a sorted list in O(log n). bisectleft(a, x) is the first index with a[i] >= x; bisectright(a, x) is the first index with a[i] > x. Use them instead of hand-rolling lower/upper bound, and with key= (3.10+) to binary search on the answer.
insort
insort inserts into a sorted list while keeping it sorted. Finding the spot is O(log n), but the list shift makes each insert O(n), so it's fine for small or read-heavy data; for heavy insert/delete workloads use a heap or sortedcontainers.SortedList.