Increment Variable Name

D

David Brochu

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
 
B

Ben Finney

David Brochu said:
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.

This has a very bad code smell. What problem are you trying to solve
by doing this? I strongly suspect there's a better solution.
 
D

Diez B. Roggisch

David said:
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
 
G

Grant Edwards

David said:
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

That assumes that the OPs "list" is actually a list of tumples:

[(1,firstvalue),(2,secondvalue), (3, thirdvalue), ...]

I'd adjust my thinking (regarding 0/1 based counting) and just
use a list or a tuple:

var = list(values)
or
var = tuple(values)

In either case, you now have

var[0], var[1], var[2], var[3], ...



If you insist on numbers starting at 1, then a dict would work:

var = {}
for i,value in itertools.enumerate(itertools.count(1), values):
var = value

now you have

var[1], var[2], var[3], var[4], ...
 
D

Diez B. Roggisch

Grant said:
David said:
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

That assumes that the OPs "list" is actually a list of tumples:

Tumples? :)

I forgot the enumerate...

Diez
 
J

janislaw

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?:
{'__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}'spam'
 
P

Paul Hankin

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.

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.
 
T

Terry Reedy

> 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.

It currently works at module level, where locals() is globals(). But then,
one might as well write globals()['var'+str(1)] = "spam". But in Python,
one is almost certainly better to use a collection object than this idiom
(indefinite multiple numbered variables) imported from languages which do
not have them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top