[portland] Sorting A List of Tuples
jason kirtland
jek at discorporate.us
Thu Nov 8 19:14:56 CET 2007
Rich Shepard wrote:
> On Thu, 8 Nov 2007, jason kirtland wrote:
>
>> The array 'sort' method and the 'sorted' function both take an optional
>> 'key' function. The function is applied to each item in the list and the
>> result is used for comparison instead of the list value itself.
>>
>> If you only want to consider the second item in the tuple, the operator
>> module's 'itemgetter' is perfect:
>
> Thanks very much, Jason.
Also, there's a good sorting discussion at python.org:
http://wiki.python.org/moin/HowTo/Sorting
It has some gems, for example:
>>> l = ['A', 'b', 'C', 'd']
>>> sorted(l)
['A', 'C', 'b', 'd']
>>> sorted(l, key=str.lower)
['A', 'b', 'C', 'd']
-j
More information about the Portland
mailing list