DGDD
DGDD

Reputation: 1390

Assign a value to a list of variables using a loop

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

Upvotes: 1

Views: 877

Answers (4)

Ranjeet R Patil
Ranjeet R Patil

Reputation: 491

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

Upvotes: 1

tobias_k
tobias_k

Reputation: 82889

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.

Upvotes: 0

Lev Levitsky
Lev Levitsky

Reputation: 65791

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

Upvotes: 1

poke
poke

Reputation: 387557

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

Upvotes: 2

Related Questions