append to non-existing list

Juho Schultz juho.schultz at helsinki.fi
Wed Nov 9 08:19:29 EST 2005


Yves Glodt wrote:
> Hello,
> 
> if I do this:
> 
> for row in sqlsth:
> ________pkcolumns.append(row[0].strip())
> ________etc
> 
> 
> without a prior:
> 
> pkcolumns = [];
> 
> 
> I get this error on first iteration:
> UnboundLocalError: local variable 'pkcolums' referenced before assignment
> 
> I guess that's normal as it's the way python works...?!?
> 
> My question is: Is there no way to append to a non existing list?
> 
> I am lazy for declaring it first, IMHO it bloats the code, and (don't 
> know if it's good to say that here) where I come from (php) I was used 
> to not-needing it...
> 
> 
> regards,
> Yves

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]



More information about the Python-list mailing list