key function

A function given as key= to sorted, sort, min or max, which turns each item into the value to compare by.

In more detail

Python calls the key function once per item and sorts by the results, so an expensive key is not recomputed at every comparison. For example, key=len sorts by length and key=str.lower sorts ignoring case. Returning a tuple sorts by several fields at once.

Related terms

Taught in

References