[Tutor] Sorting Nested Lists

Sarma Tangirala tvssarma.omega9 at gmail.com
Mon Jan 9 13:31:37 CET 2012


Hi list,

I was banging my head about a pythonic way of doing the following,

Given a nested list, how do I sort the uppermost list based on one key and
when a special condition occurs a sort on another key should be performed?

For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my example
as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second value and
in case they are equal, reverse sort on the first value.

I tried doing this using sorted and using a custom cmp function but not
sure about how to define the cmp function.

-- 
Sarma Tangirala,
Class of 2012,
Department of Information Science and Technology,
College of Engineering Guindy - Anna University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120109/487d9353/attachment.html>


More information about the Tutor mailing list