[Tutor] Sorting 2-d data

Dirk Wangsadirdja dirk at pensiun.com
Sun Sep 13 17:25:17 CEST 2009


Lie Ryan wrote:
> 
> if you have data like this:
> mylist = [
>     [3, 4, 3, 2, 1],
>     [2, 1, 1, 1, 1],
>     [4, 2, 2, 1, 2],
>     [1, 3, 1, 1, 1],
> ]
> 
> you can use mylist.sort(key=lambda x: x[0])
> 
> sorted(mylist, key=lambda x: x[0]) works as well if you need a new list
> 

I have a further question. If I want to sort the list not based on first 
element, but the second, third and so on, then using the lambda works 
nicely.

But for sorting the list with the first element as key, I tried it using 
just mylist.sort() without the lambda, and its working also. Then why 
use the lambda?

Sorry, I'm using python3. so maybe it won't work on python2 with just 
mylist.sort()?


More information about the Tutor mailing list