"groupby" is brilliant!

Frank Millman frank at chagford.com
Tue Jun 13 05:08:01 EDT 2006


Paul McGuire wrote:
> >
> > reader = csv.reader(open('trans.csv', 'rb'))
> > rows = []
> > for row in reader:
> >     rows.append(row)
> >
>
> This is untested, but you might think about converting your explicit "for...
> append" loop into either a list comp,
>
>     rows = [row for row in reader]
>
> or just a plain list constructor:
>
>     rows = list(reader)
>
> Neh?
>
> -- Paul
>

Yup, they both work fine.

There may be times when you want to massage the data before appending
it, in which case you obviously have to do it the long way. Otherwise
these are definitely neater, the last one especially.

You could even do it as a one-liner -
    rows = list(csv.reader(open('trans.csv', 'rb')))

It still looks perfectly readable to me.

Thanks

Frank




More information about the Python-list mailing list