Python bisect: insort(a, x, key=f) applies the key to x, but bisect_left(a, x, key=f) does not ("TypeError: 'int' object is not subscriptable")
With a sorted list of dicts and key=lambda d: d['k'], bisect.bisect_left(a, 3, key=...) works because x must already be the key value, so people assume bisect.insort(a, 3, key=...) works too. It raises TypeError: 'int' object is not subscriptable, because insort calls key(x) and therefore needs a full element. The reverse mistake fails as well: bisect_left(a, {'k': 3}, key=...) raises TypeError: '<' not supported between instances of 'int' and 'dict'. The asymmetry is documented but surprising, and it shows up as soon as code switches between searching and inserting. Fix: pass the key value to bisect_left/bisect_right and the whole element to insort_left/insort_right (e.g. bisect.insort(a, {'k': 3}, key=f)). Same on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.