Primary
''key function'' ⚬|Definition|1st|20260605182851-00-⌔
Glossary — Python 3 documentation#term-key-function
key function
A key function or collation function is a callable that returns a value used for sorting or ordering. For example,
locale.strxfrm()is used to produce a sort key that is aware of locale specific sort conventions.A number of tools in Python accept key functions to control how elements are ordered or grouped. They include
min(),max(),sorted(),list.sort(),heapq.merge(),heapq.nsmallest(),heapq.nlargest(), anditertools.groupby().There are several ways to create a key function. For example. the
str.casefold()method can serve as a key function for case insensitive sorts. Alternatively, a key function can be built from alambdaexpression such aslambda r: (r[0], r[2]). Also,operator.attrgetter(),operator.itemgetter(), andoperator.methodcaller()are three key function constructors. See the Sorting HOW TO for examples of how to create and use key functions.Printed 2026-06-28.
(echo:: @ ᯤ)
Link to original
Secondary
• • •