Sorting lists

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Nov 17 05:30:31 EST 2008


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.

------------
In C# a syntax for lambdas may become a little shorter, but the
parsing may become less easy:
L2.sort(key = sub => sub[1])

In Mathematica they use the & to define lambdas, and #1, #2, etc, to
denote the arguments.

Bye,
bearophile



More information about the Python-list mailing list