17
var_list = [one, two, three]
num = 1
for var in var_list:
    var = num
    num += 1

The above gives me an error that 'one' doesn't exist. Can you not assign in this way? I want to assign an incrementing number for each var in the list. So I want the equivalent of

one = 1
two = 2
three = 3

but without having to manually create and initialize all variables. How can I do this?

wovano
  • 4,543
  • 5
  • 22
  • 49
johnharris85
  • 17,264
  • 5
  • 48
  • 52
  • For people finding this question later: there's a good chance that the *most appropriate solution to your problem* is at https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables . If you find yourself wanting to make this kind of assignment, it's probably because your existing variables are laid out inappropriately (or the new ones you want to create shouldn't be done the way you're thinking of doing them). – Karl Knechtel Feb 05 '21 at 06:19

5 Answers5

30

You can access the dictionary of global variables with the globals() built-in function. The dictionary uses strings for keys, which means, you can create variables with names given as strings at run-time, like this:

>>> var_names = ["one", "two", "three"]
>>> count = 1
>>> for name in var_names:
...  globals()[name] = count
...  count += 1
... 
>>> one
1
>>> two
2
>>> three
3
>>> globals()[raw_input()] = 42
answer
>>> answer
42

Recommended reading

Michael Cornelius
  • 1,516
  • 1
  • 11
  • 11
13

Python variables are names for values. They don't really "contain" the values.

for var in var_list: causes var to become a name for each element of the list, in turn. Inside the loop, var = num does not affect the list: instead, it causes var to cease to be a name for the list element, and instead start being a name for whatever num currently names.

Similarly, when you create the list, if one, two and three aren't already names for values, then you can't use them like that, because you are asking to create a list of the values that have those names (and then cause var_list to be a name for that list). Note that the list doesn't really contain the values, either: it references, i.e. shares them.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
7

No, it doesn't work like that.

You can try:

one, two, three = range(1, 4)

This work by defining the variables in a multiple assignment. Just as you can use a, b = 1, 2. This will unroll the range and assign its values to the LHS variables, so it looks like your loop (except that it works).

Another option (which I wouldn't recommend in a real program...) would be to introduce the variable names in an exec statement:

names = ['one', 'two', 'three']
num = 1
for name in names:
    exec "%s = %s" % (name, num)
    num += 1
print one, two, three                  
TryPyPy
  • 6,214
  • 5
  • 35
  • 63
5

A variable doesn't exist until you create it. Simply referencing a variable isn't enough to create it. When you do [one, two, three] you are referencing the variables one, two and three before they are created.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

I would simply use list comprehension.

one, two, three = range(1, 4)
one, two, three = [val for val in ['one', 'two', 'three']]
v1, v2, v3 = [func(idx) for idx in range(1, 4)]