gh-113773: add list.index() "key" named argument by kristjanvalur · Pull Request #113772 · python/cpython (original) (raw)

This PR adds a keyword-only key argument to list.index().
When provided, each list item is transformed by the key callable before being compared to value, thus allowing
a functional-style search of an item in a list:

data = [(i, chr(i)) for i in range(100)] index_of_a = data.index("a", key=lambda i: i[1])

This is based on an idea by @rhettinger from the discussion in PR #112498, where a case for finding an object in a list via
keyed lookup is made. This PR applies that idea directly to the list object underlying the heap in question.

list.index() is not explicitly documented anywhere, so there is no doc update.