[Tutor] string to list

Kent Johnson kent37 at tds.net
Thu Feb 11 15:11:30 CET 2010


On Thu, Feb 11, 2010 at 8:37 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> 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))

Ok, I thought you meant make a new list. These examples don't swap
anything back...

You can also use
data.sort(key=operator.itemgetter(1, 0))

Kent


More information about the Tutor mailing list