Store variable name in another variable

Larry Bates larry.bates at websafe.com
Thu Apr 26 09:06:15 EDT 2007


loial wrote:
> I need to store a list of variable names in a dictionary or list. I
> then later need to retrieve the names of the variables and get the
> values from the named variables. The named variables will already have
> been created and given a value.
> 
> I hope thats clear!!!
> 
> How can I do this?
> 
Use a dictionary.  Example:

var1=10
var2="abc"
var2=1.2

vardict['var1']=var1
vardict['var2']=var2
vardict['var3']=var3

print vardict['var1']
print vardict['var2']
print vardict['var3']

-Larry



More information about the Python-list mailing list