Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.
>>> import operator
>>>
>>> a_list = [('Tom', 23), ('Dick', 45), ('Harry', 33)]
>>> sorted(a_list, key=operator.itemgetter(1), reverse=True)[0]
('Dick', 45)
I could do it quite verbosely...
>>> age = 0
>>> oldest = None
>>> for person in a_list:
... if person[1] > age:
... age = person[1]
... oldest = person
...
>>> oldest
('Dick', 45)