sort the list

Neil Hodgson nyamatongwe+thunder at gmail.com
Tue Nov 22 16:00:02 EST 2005


Nick Craig-Wood:

> Since no-one mentioned it and its a favourite of mine, you can use the
> decorate-sort-undecorate method, or "Schwartzian Transform"

    That is what the aforementioned key argument to sort is: a built-in 
decorate-sort-undecorate.

 >>> lst = [[1,4],[3,9],[2,5],[3,2]]
 >>> print lst
[[1, 4], [3, 9], [2, 5], [3, 2]]
 >>> lst.sort(key=lambda x: x[1])
 >>> print lst
[[3, 2], [1, 4], [2, 5], [3, 9]]

    Neil



More information about the Python-list mailing list