append to non-existing list

Yves Glodt y.glodt at sitasoftware.lu
Wed Nov 9 08:35:41 EST 2005


Juho Schultz wrote:
> 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]

I will look into this, maybe it's what I need, thanks!



More information about the Python-list mailing list