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()?
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to