[Tutor] create 1000 000 variables

Alan Gauld alan.gauld at freenet.co.uk
Sat Jul 15 19:59:32 CEST 2006


> In fact I want to create a list of variables from the list of strings
> 
> Example: ['var1', 'var2', 'var3'....] - list of strings
> And I need to create variables var1, var2, var3 named as strings in the
> list, and:
> var1 == 'var1'
> var2 == 'var2'
> var3 == 'var3'
> 
> How to do this using only my list and "for i in "???

Lets back up a stage.
Why do you think you need the variables?

Variables in Python are just a reference to an object/value.
Lists also contain references to objects/values.
So if you have the list why would you also need variables to 
reference the same objects? What do you think you could 
do differently with the variables that you can't do with the 
list reference?

Here is an ASCII art picture (pick your font carefully!)

           index onject  variable
myList = [
            0    =>  A <= var_0
            1    =>  B <= var_1
            2    =>  C <= var_2
            3    =>  D <= var_3
            4    =>  E <= var_4
         ]

What difference do you perceive between 

var_2 

and 

myList[2]

Clue - There isn't any...

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060715/cfe182ad/attachment.html 


More information about the Tutor mailing list