> 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?
There is a little difference between those two variations.
Example:
>>> sorted([[1,2],[1,3],[1,1]])
[[1, 1], [1, 2], [1, 3]]
>>> sorted([[1,2],[1,3],[1,1]], key=lambda x:x[0])
[[1, 2], [1, 3], [1, 1]]
For sorting it is necessary to compare items. So for example [1,1] is
compared to [1,2]
As you see:
>>> [1,1] < [1,2]
True
If you apply the lambda things change:
>>> (lambda x: x[0])([1,1])<(lambda x:x[0])([1,2])
False
>>> (lambda x: x[0])([1,1])==(lambda x:x[0])([1,2])
True
This is because now only the first item of the list is compared.
- Patrick
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor