Increment Variable Name

janislaw wicijowski at gmail.com
Thu Jan 24 07:02:45 EST 2008


On Jan 23, 11:45 pm, David Brochu <brochu... at gmail.com> wrote:
> 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.
>
> Thanks

Do you want to do this?:

>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'pywin': <module 'pywin' from 'C:\Python25\Lib\site-
packages\pythonwin\pywin\__init__.pyc'>, '__doc__': None}
>>> locals()['var'+str(1)] = "spam"
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'var1': 'spam', 'pywin': <module 'pywin' from 'C:
\Python25\Lib\site-packages\pythonwin\pywin\__init__.pyc'>, '__doc__':
None}
>>> var1
'spam'



More information about the Python-list mailing list