Given a tuple list, i can find the max of the 1st element in the tuple list by:
>>> a,b,c,d = (4,"foo"),(9,"bar"),(241,"foobar"), (1,"barfoo")
>>> print max([a,b,c,d])
(241,"foobar")
But how about finding the max of the 2nd element? and how about the max of a string?
Use the
keyparameter:The
max()of a string is based on the byte values of the string;'a'is higher than'A'becauseord('a')is higher thanord('A').For your example input,
(241,"foobar")is still the max value, because'f'>'b'and'foobar'is longer (more values) than'foo', and'b'>''(withbbeing the character following the lettersfooin the longer string).