[Tutor] Naming variables

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jan 19 14:59:55 CET 2014


On 19/01/2014 13:23, spir wrote:
> On 01/18/2014 07:20 PM, Pierre Dagenais wrote:
>> Hello,
>>
>> I wish to fill a list called years[] with a hundred lists called
>> year1900[], year1901[], year1902[], ..., year1999[]. That is too much
>> typing of course. Any way of doing this in a loop? I've tried stuff like
>> ("year" + str(1900)) = [0,0] but nothing works.
>> Any solution?
>>
>> Thank you,
>>
>> PierreD.
>
> I think Danny & Wiktor's solutions are the right ones. Danny's is faster
> a simpler (because of direct array indexing), but does not allow giving
> the true year "names" (actually numbers). Wiktor more correctly matches
> you expectation bt is a bit slower because we're searching individual
> years in a dict.
>

I suspect that the speed difference between accessing a list by direct 
indexing and looking up something in a dict is negligible.  I'll happily 
leave Steven D'Aprano to supply us with the actual figures from the 
timeit module :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list