[Tutor] string to list

Stefan Behnel stefan_ml at behnel.de
Thu Feb 11 14:37:34 CET 2010


Kent Johnson, 11.02.2010 14:16:
> On Thu, Feb 11, 2010 at 4:44 AM, Stefan Behnel wrote:
> 
>> 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.
> 
> No, that is the old decorate-sort-undecorate idiom which has been
> replaced by the key= parameter to sort.

Nothing keeps you from writing

    data.sort(key=lambda x:x[::-1])

or

    data.sort(key=operator.itemgetter(slice(None,None,-1))

(which is equivalent)

Stefan



More information about the Tutor mailing list