sort the list
Daniel Schüle
uval at rz.uni-karlsruhe.de
Mon Nov 21 06:59:35 EST 2005
Shi Mu wrote:
> I have a list like [[1,4],[3,9],[2,5],[3,2]]. How can I sort the list
> based on the second value in the item?
> That is,
> I want the list to be:
> [[3,2],[1,4],[2,5],[3,9]]
>>> lst = [[1,4],[3,9],[2,5],[3,2]]
>>> lst
[[1, 4], [3, 9], [2, 5], [3, 2]]
>>>
>>>
>>> lst.sort(cmp = lambda x,y: cmp(x[1], y[1]))
>>> lst
[[3, 2], [1, 4], [2, 5], [3, 9]]
>>>
works for Python 2.4
in earlier Pythons just let cmp = .. away
Regards, Daniel
More information about the Python-list
mailing list