Specify the key
argument in the sorted
function.
>>> tuple1=[(1, 3), (3, 2), (2, 1)]
>>> output = sorted(tuple1, key=lambda x: x[-1])
>>> print output
[(2, 1), (3, 2), (1, 3)]
The sorted
function (as well as the list.sort
method) has an optional key
argument which specifies what to sort the list on.