Sorting lists

Terry Reedy tjreedy at udel.edu
Mon Nov 17 20:36:03 CET 2008


bearophileHUGS at lycos.com wrote:
> Chris Rebert:
>> You use the `key` argument to .sort():
>> L2.sort(key=lambda item: item[1])
> 
> I like the lambda because it's a very readable solution that doesn't
> require the std lib and it doesn't force the programmer (and the
> person that reads the code) to learn yet another thing/function.
> 
> But I can suggest to also look for operator.itemgetter.

Since itemgetter is builtin, it will probably run faster, though the 
O(nlogn) time for sorting will override the O(n) time for key calls.




More information about the Python-list mailing list