Increment Variable Name

Diez B. Roggisch deets at nospam.web.de
Wed Jan 23 18:27:44 EST 2008


David Brochu schrieb:
> This is probably really trivial but I'm stumped.... :-(
> 
> Does anyone know how to increment a variable name?
> 
> For example:
> 
> I know the length of a list and I want to pass each element of a list to 
> a unique variable, thus I want to increment variable names. If the list 
> length = 4, i want to have the following variables: var1, var2, var3, var4.
> 

Use a dictionary

value_dict = {}

for i, value in values:
    value_dict["var%i" % i] = value


Diez



More information about the Python-list mailing list