[Tutor] Sorting a list of lists aka nested lists

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Aug 13 22:38:21 CEST 2005



On Sat, 13 Aug 2005, Jim Roush wrote:

> I have a python script that creates a list of lists like so:
>
>     Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] )
>
> After Quant is created, I want to sort it by MTD.  If I use a simple
> Quant.sort(), I assume its going to sort by 'db_ticker' which is not
> what I want.
>
> I've been trying to figure out how to use the cmp() in a list sort
> method and I have failed to understand.


Hi Jim,

AMK has written a nice mini-tutorial on how to use a list's sort()
effectively:

    http://www.amk.ca/python/howto/sorting/sorting.html

Does his tutorial make sense, or are there parts in there that are
baffling?  Please feel free to ask questions on it, and we'll try to help.



More information about the Tutor mailing list