Optimizing list processing

MRAB python at mrabarnett.plus.com
Wed Dec 11 21:09:42 EST 2013


On 12/12/2013 01:43, Steven D'Aprano wrote:
> On Thu, 12 Dec 2013 00:59:42 +0000, MRAB wrote:
>
>>> table = [(x, i) for i,x in enumerate(iterable)]
>>> table.sort()
>>
>> This looks wrong to me:
>>
>>> for x, i in table:
>>>      table[i] = x
>
> Yes, you're right, I over-simplified the example, and in doing so
> introduced a bug. What I actually use is:
>
> for i, x in enumerate(table):
>      table[i] = x[1]
>
> modulo any further bugs introduced while copying and pasting :-)
>
How does this compare:

table = list(iterable)
indexes = list(range(len(table)))
indexes.sort(key=table.__getitem__)




More information about the Python-list mailing list