bisect
Find insertion positions in an already sorted list. Searching takes logarithmic time; insertion still moves list elements and can take linear time.
bisect aliases bisect_right; insort aliases insort_right.
The search interval is [lo, hi), with hi=None meaning len(a).
Keep those bounds within the list. There is no key= parameter.
from bisect import bisect_left, bisect_right, insort
scores = [10, 20, 20, 40]
assert bisect_left(scores, 20) == 1
assert bisect_right(scores, 20) == 3
insort(scores, 30)
assert scores == [10, 20, 20, 30, 40]
Implementation: bisect.py.