Optimizing list processing

MRAB python at mrabarnett.plus.com
Thu Dec 12 08:32:11 EST 2013


On 12/12/2013 12:25, Chris Angelico wrote:
> On Thu, Dec 12, 2013 at 11:08 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> P.S. The algorithm I'm working on is a way of generating index and rank
>> tables. Not that it really matters -- what matters is determining whether
>> or not to shift from "make a copy of the list" to "modify the list in
>> place".
>
> So you're currently looking at...
>
> if len(table) < ?????:
>      table = [i for x,i in table]
> else:
>      for x, i in table:
>          table[i] = x
>
>
> Can I throw a spanner [1] in the works with other suggestions to try timing?
>
> table[:] = [i for x,i in table]  # Does slice assignment get optimized?
>
[snip]

If you're trying that, you could also try:

table[:] = (i for x,i in table)




More information about the Python-list mailing list