[Tutor] string to list

Stefan Behnel stefan_ml at behnel.de
Thu Feb 11 10:44:08 CET 2010


Owain Clarke, 10.02.2010 17:57:
> data.sort(key=lambda x:x[0])
> data.sort(key=lambda x:x[1])

Two things to note:

1) you can use the operator module, specifically operator.itemgetter

2) given that you have lists as items in the 'data' list, it's enough to
call sort() once, as the comparison of lists is defined as the comparison
of each item to the corresponding item of the other list. If you want to
sort based on the second item before the first item, it's best to exchange
both items before sorting and swap them back afterwards.

Stefan



More information about the Tutor mailing list