Increment Variable Name

Paul Hankin paul.hankin at gmail.com
Thu Jan 24 07:35:34 EST 2008


On Jan 24, 12:02 pm, janislaw <wicijow... at gmail.com> wrote:
> 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()['var'+str(1)] = "spam"

As I recently learnt in this newsgroup, that's not guaranteed to work.
>From http://docs.python.org/lib/built-in-funcs.html

Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter.

--
Paul Hankin



More information about the Python-list mailing list