Getting values out of a CSV

Kelvie Wong kelvie at ieee.org
Fri Jul 13 05:34:24 EDT 2007


On 7/12/07, Daniel <no at no.no> wrote:
> On Fri, 13 Jul 2007 08:51:25 +0300, Gabriel Genellina
> <gagsl-py2 at yahoo.com.ar> wrote:
> >> data = [row for row in csv.reader(open('some.csv', 'rb'))
> >
> > Note that every time you see [x for x in ...] with no condition, you can
> > write list(...) instead - more clear, and faster.
> >
> > data = list(csv.reader(open('some.csv', 'rb')))
>
> Clearer? Maybe, but list comprehensions are clearer (at least for me)
>
> Faster? No. List Comprehensions are faster.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

kelvie at valour pdfps $ python -m timeit -c 'data = list(open("make.ps"))'
100 loops, best of 3: 7.5 msec per loop
kelvie at valour pdfps $ python -m timeit -c 'data = [line for line in
open("make.ps")]'
100 loops, best of 3: 9.2 msec per loop

On my system just putting into a list is faster.  I think this is
because you don't need to assign each line to the variable 'line' each
time in the former case.

I, too, think it's faster to just use list() instead of 'line for line
in iterable', as it seems kind of redundant.

-- 
Kelvie



More information about the Python-list mailing list