good way to do side effects on lists?

bruno modulix onurb at xiludom.gro
Thu Sep 23 16:18:59 EDT 2004


danny a écrit :
> Michael Hoffman <m.h.3.9.1.without.dots.at.cam.ac.uk at example.com> wrote in message news:<cit221$a33$1 at pegasus.csx.cam.ac.uk>...
>  
> 
>>But with all that set-up it's not really a one-liner. And what's wrong with
>>your first proposal anyway?
> 
> 
> Nothing is really wrong with it. I assume it runs a little slower than
> something native, 

Dont assume, test !-)

def fun(arg):
     if arg > 500 :
             a = 42
     else:
             b = 84

def withfor(alist):
     for item in alist: fun(item)

def withmap(alist):
     map(fun, alist)

alist = range(1000)

from timeit import Timer

t1 = Timer("withfor(alist)", "from __main__ import withfor, alist")
t2 = Timer("withmap(alist)", "from __main__ import withmap, alist")

t1.repeat(3, 1000)
[1.9760220050811768, 1.975412130355835, 1.9746799468994141]

t2.repeat(3, 1000)
[2.1670758724212646, 2.1661739349365234, 2.1647889614105225]

HTH
Bruno



More information about the Python-list mailing list