16/41
Part 3 of 7 · bisectLearn and do

With key=, pass the field you search for, not a whole record

Since Python 3.10, every bisect function takes key=, so you can search a list of records by one field. The search applies the key to the list's items, but not to the value you search for. So pass the field, not a record:

history = [(100, "a"), (130, "b"), (130, "c"), (220, "d")]
i = bisect_right(history, 130, key=lambda change: change[0])
print(i, history[i - 1])
3 (130, 'c')

Pass (130,) there instead, and it fails with TypeError: '<' not supported between instances of 'tuple' and 'int'.

In the editor, write value_at(history, t) with bisect_right and key=. It reads a history at five times.