append to non-existing list

bonono at gmail.com bonono at gmail.com
Wed Nov 9 08:23:54 EST 2005


Juho Schultz wrote:
> Yves Glodt wrote:
> > Hello,
> >
> > if I do this:
> >
> > for row in sqlsth:
> > ________pkcolumns.append(row[0].strip())
> > ________etc
> >
> You mean you want to type "pkcolumns" only once to keep your code short?
> Would something like this be useful?
>
> pkcolumns = [row.strip() for row in sqlsth]

I don't think this is the same as his original code though. There is an
"etc" there. Your version is cleaner and easier to understand but it
can mean another extra pass to go through the result and if sqlsth is
an iterable, it needs to be saved away before iterating it.




More information about the Python-list mailing list