-1

I have seen duplicates of this, but the original question was never answered: is it possible to assign values to multiple variables using a loop or some other method that would be more efficient that writing out all the variables in hard code.

NOTE: I do not want to use dictionaries or list, I want to use variables. My question is whether it is possible to assign multiple variables at once using a loop or other method, not using dictionaries.

If this is not possible just say so, but I really don't want to see another answer describing how to use lists or dictionaries.

Duplicates: Python: assigning multiple variables at once

Assign Many Variables at Once, Python

Mike Smith
  • 527
  • 2
  • 6
  • 20
  • 2
    If you mention this duplicate question, can you link to it? – chluebi Jan 22 '20 at 19:33
  • 1
    That duplicate you're probably talking about includes an answer that creates multiple variables using a for loop. But I've never seen a real world use case that warrants its use. – Sayse Jan 22 '20 at 19:35
  • 1
    Does this answer your question? [Adding element to a dictionary in python?](https://stackoverflow.com/questions/40776193/adding-element-to-a-dictionary-in-python) – chluebi Jan 22 '20 at 19:36
  • I mentioned in my question that I don't want to use dictionaries. I want to learn how to create variables. @Sayse and AMC, which duplicate is it? – Mike Smith Jan 22 '20 at 19:41
  • 1
    [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Sayse Jan 22 '20 at 19:41
  • Are you talking about using the `globals()`. I'll try that out, thanks! – Mike Smith Jan 22 '20 at 19:45
  • 1
    _I want to learn how to create variables._ Please be more specific. What you're asking for can be done, yes, but isn't necessary the vast majority of the time. – AMC Jan 22 '20 at 22:49

1 Answers1

1

DISCLAIMER: The global()[] function that I talk about here may have more uses/parameters/arguments, but I am just telling you what I know. You can experiment with it on your own


Ok, now that I have found the answer, I'll post it here for future members to view. Note that, as mentioned above, this technique is seldom used, but it's great to know for those corner cases.

The crucial function here is the global()[] function in which you input a string and it turns it into a variable name. You leave the () completely empty, and enter the string into the []. Now, this may seem useless, but one thing you can do is,

for i in range(100):
    global()["Var"+str(i)] = i

Now you have 100 variables of the form Var# where # is the number, and, in this case, the value of the variable. This is a very, very simple case, and because of this extra flexibility, there are several things you can do with this.

Again, you will probably use this very few times while programming in python, since using extra lists of strings and values and then using this to create variables is unnecessary and inefficient, but, in the right places, this can save you a lot of time. Just comment if you have any questions since I am not that good at explaining things.

Mike Smith
  • 527
  • 2
  • 6
  • 20
  • _but, in the right places, this can save you a lot of time._ Could you give an example? – AMC Feb 06 '20 at 00:27