1

I can do this in C, but I haven't come across it in Python.

Say I have a few variables:

variable1 = None
variable2 = None
variable3 = None
variable4 = None
variable5 = None

and I have list of values [1, 2, 3, 4]

how can I assign the values to the variables with a single loop so I get the following result:

variable1 = 1
variable2 = 2
variable3 = 3
variable4 = 4
variable5 = None
1
  • 2
    Why not use a list instead? Commented Dec 22, 2013 at 17:29

4 Answers 4

2

While you technically can modify local variables, doing so is very discouraged. Instead, you should store those values in a dictionary instead:

variables = {
    variable1: None,
    variable2: None,
    variable3: None,
    variable4: None,
    variable5: None
}

values = [1, 2, 3, 4]

for i, value in enumerate(values):
    variables['variable' + (i + 1)] = value

But of course, if all those variables differentiate is the number, you can simply use a list as well:

# This will create a list with 5 None values, i.e. variables[0] .. variables[4]
variables = [None] * 5

for i, value in enumerate(values):
    variables[i] = value
Sign up to request clarification or add additional context in comments.

Comments

1

You should indeed consider using a list instead, but if you are sure about what you are doing, try:

var1, var2, var3, var4, var5 = my_list + [None]*(5-len(my_list))

Comments

1
for x in range(1, 6):
    if x == 5: 
        globals()['variable%s' % x] = "None"
    else:
        globals()['variable%s' % x] = x

for x in range(1, 6):
    print(globals()['variable' + str(x)]) #

Note

The globals() method returns the dictionary of the current global symbol table. A symbol table is a data structure maintained by a compiler which contains all necessary information about the program. These include variable names, methods, classes, etc. There are mainly two kinds of symbol table.

  1. Local symbol table
  2. Global symbol table

Local symbol table stores all information related to the local scope of the program, and is accessed in Python using locals() method. The local scope could be within a function, within a class, etc. Likewise, a Global symbol table stores all information related to the global scope of the program, and is accessed in Python using globals() method. The global scope contains all functions, variables that are not associated with any class or function.

source: https://www.programiz.com/python-programming/methods/built-in/globals

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Thanks, did provide explanation to support the answer.
0

I would not recommend doing this, but if you really must, you could use globals() to get a dictionary of defined variables and functions, to access and to modify them.

var1 = var2 = var3 = var4 = var5 = None
for i, v in enumerate([1, 2, 3, 4]):
    globals()["var%d" % (i+1)] = v
print var1, var2, var3, var4, var5

But, really, you should rather use a list or a dict instead, as suggested in the other answers.

2 Comments

globals() yes, although as you say you shouldn't; locals() no. The latter isn't guaranteed to work at all, as the docs you linked explain.
@DSM Thanks for the hint, somehow I totally missed that Info box. Edited to suggest only globals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.