# As usually just making some data for an experiment
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
# And here... We're define small single-expression function on fly to operate
# with values of list and chose which one would be used to sort list.
pairs.sort(key=lambda pair: pair[1])
print(pairs)
print
# It actually would be pretty equal to following way of doing the same.
# Redefine initial data, cause our list is already sorted.
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def sortItem(pair):
return pair[1]
pairs.sort(key=sortItem)
print pairs
# The result is just the same, but our syntax is shorter and self-descriptive.
# But remember, lambdax could handle only single expression, for more powerful
# caluclation you still need a full function.