-. cmp . . key.
lambda . , def , .
my_func = lambda x: x + 1
, , x x + 1. lambda x, y=1: x + y , x, y 1 x + y. , , def, , .
key , sorted , .
list_ = ['a', 'b', 'c']
sorted(list_, key=lambda x: 1)
. , . , .
,
dict s. dicts s? .username.
a > , -
users = [{'name': 'Tom Cruise', 'username': user234234234, 'reputation': 1},
{'name': 'Aaron Sterling', 'username': 'aaronasterling', 'reputation': 11725}]
, , , :
sorted(users, key=lambda x: x['reputation'])
, 'reputation' . lambdas . operator.itemgetter - , .
operator.itemgetter , .
f = operator.itemgetter('name', 'username') ,
lambda d: (d['name'], d['username']) , , lambda.
, dict ,
sorted(list_of_dicts, operator.itemgetter('name', 'username'))
, -.