* Carlos Grohmann:
Hello all

I am testing my code with list comprehensions against for loops.

the loop:

    dipList=[float(val[1]) for val in datalist]
    dip1=[]
    for dp in dipList:
        if dp == 90:
            dip1.append(dp - 0.01)
        else:
            dip1.append(dp)

listcomp:

    dipList=[float(val[1]) for val in datalist]
    dip1=[(dp, dp-0.01)[dp==90.0] for dp in dipList]


Tenting the time spent by each approach (using time.clock()), with a
file with about 100,000 entries, I get 0.03s for the loop and 0.05s
for the listcomp.

thoughts?

In the list comprehension you're constructing n tuples that you're not constructing in the loop.

Have you tried this with

  dip1 = [dp - 0.01 if dp == 90 else dp for dp in dipList]

?


Cheers & hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to