0

I have a simple question of beginner.

How to make this code work :

variable0 = 0

...
variable9 = 9

I tried :

for j in range(9):
    variable"{}".format(j) = j

and others similar things with eval() and exec() functions.

Thanks a lot !

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 6
    You don't *want* lots of similarly named variables like this; use a list instead: `variable = list(range(10))`. – chepner Mar 18 '19 at 14:00
  • 3
    Why do you need this? Please checkout [what is an XY question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - it seems unlikely this is your needed solution. – kabanus Mar 18 '19 at 14:00
  • Dynamically creating variables is indeed possible in Python, but you probably do not want that. The good question here is *why should we use dynamic variables instead of lists, arrays or dictionaries*. Unless you can give an acceptable answer to that question, I will only say here: stick to a Python container. – Serge Ballesta Mar 18 '19 at 14:34

6 Answers6

5

Why not use a list instead, together with list comprehension:

variable = [i for i in range(10)]

Then you could use variable[0] for your "variable0", etc.


Or, as mentioned in a few comments

variable = list(range(10))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

If you really want to do this you can do:

for j in range(9):
    exec("variable{} = j".format(j))

But as others have said, you probably shouldn't.

Toby Petty
  • 4,431
  • 1
  • 17
  • 29
  • 4
    To be honest this is the actual answer, though unlikely what OP really needs. – kabanus Mar 18 '19 at 14:03
  • 2
    You definitely shouldn't! @DirtyBit SO isn't about just giving the exact answer OP wants, it's also about guiding them to what the *should* do. – FHTMitchell Mar 18 '19 at 14:15
  • 1
    @FHTMitchell Of course it is about guiding them to the right approach, but sometimes people need specific behaviour. For example changing variable names dynmically if he/she needs 10 different variables while assigning values and it is impossible to do it with the list approach. I think it is better to give the exact answer if possible but to **advise** to read or try some different approach or to tell that this approach is not correct. – ammar Mar 18 '19 at 14:25
1

If you're using numbers, use a list:

items = [1, 2, 3, 4, ...]

or

items = range(10)

then you can look up items[0], items[2], etc

If you want to use something else to identify items, use a dictionary:

items = {
    1: 1,
    2: 2,
    'foo': 'bar'
    ...
}

You can look up with items[0], items['foo'], etc

You can also use list comprehension or dictionary comprehension to define your data structures, e.g.

items = {i: i for i in range(10)}
ben_nuttall
  • 859
  • 10
  • 20
0

I think the use of a dictionary is better suited for this task:

d = {"variable{}".format(i): i for i in range(10)}

print(d['variable0']) # ==> 0
print(d['variable5']) # ==> 5
print(d['variable9']) # ==> 9
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

If you really, really need this specific behaviour (changing variable names on the fly) there is a way to acomplish it. I don't recommend this solution, since there are more intuitive ones and this approach is not a very good design. Here it is:

l = locals()
counter = 0;
for i in range(9):
   l["variable" + str(counter)] = i
   counter += 1
ammar
  • 64
  • 6
-1

I wouldn't really recommend this but you could assign the globals() or locals() dictionaries depending on whether you're doing this at the module level or within a function.

for i in range(10): globals()[f'variable{i}'] = 0

def myfunc():
    for i in range(10): locals()[f'variable{i}'] = 0
Alain T.
  • 40,517
  • 4
  • 31
  • 51