[Tutor] Sorting a list of lists aka nested lists

mailing list ml.cyresse at gmail.com
Sat Aug 20 03:29:13 CEST 2005


No-one's mentioned a Schwartzian Transform yet ; ) 



On 8/15/05, Kent Johnson <kent37 at tds.net> wrote:
> jfouhy at paradise.net.nz wrote:
> > Note that in Python2.4+, you can use key= instead:
> >
> > def sortKey(lst):
> >  return lst[2]
> > Quant.sort(key=sortKey)
> >
> > This is more effient than specifying a different comparison function, because
> > the key function is only called once for each element.
> 
> And instead of defining your own function you can use itertools.itemgetter() which is intended for this purpose:
> import itertools
> Quant.sort(key=itertools.itemgetter(2))
> 
> Kent
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list