flatten a level one list

Michael Spencer mahs at telcopartners.com
Thu Jan 12 05:26:49 CET 2006


Tim Hochberg wrote:
> Michael Spencer wrote:
>>  > Robin Becker schrieb:
>>  >> Is there some smart/fast way to flatten a level one list using the
>>  >> latest iterator/generator idioms.
>> ...
>>
>> David Murmann wrote:
>>  > Some functions and timings
>> ...
> 
> Here's one more that's quite fast using Psyco, but only average without it.
> 
> 
> def flatten6():
>      n = min(len(xdata), len(ydata))
>      result = [None] * (2*n)
>      for i in xrange(n):
>              result[2*i] = xdata[i]
>              result[2*i+1] = ydata[i]
> 
> -tim
> 
Indeed:

I added yours to the list (after adding the appropriate return)

  >>> testthem()
  >>> timethem()
  flatten1(...)  702 iterations, 0.71msec per call
  flatten2(...)  641 iterations, 0.78msec per call
  flatten3(...)  346 iterations, 1.45msec per call
  flatten4(...)  1447 iterations, 345.66usec per call
  flatten5(...)  1218 iterations, 410.55usec per call
  flatten6(...)  531 iterations, 0.94msec per call
  >>>

(See earlier post for flatten1-5)

Michael




More information about the Python-list mailing list