2

If you want to create tens of thousands of variables in Python, is there a way to do this in a for statement. What I want is a code that, when given a number and a value, can create that number of variables and assign them that value.

def createVars(num, somevalue): for x in range(num): var1 = somevalue var2 = somevalue 

The idea is for the loop to continue making variables with reliable names(var3, var4, etc.) and assigning them to some given value. How could you make this system that could create variables without explicitly having to name all of them?

6
  • use either locals or globals Commented Apr 11, 2015 at 0:01
  • 2
    Have we all forgotten that mylist[0] or mylist.get('var_0') is just as real a variable as var_0? Commented Apr 11, 2015 at 0:20
  • 3
    You do not need this. Use a list. That's what lists are for. Commented Apr 11, 2015 at 0:23
  • 2
    OP, in all likelihood, you don't actually want to do this. If you need thousands of variables, put them in a container and access them like var[1] instead of var1. The way you are trying to do it is paddling against the current. Commented Apr 11, 2015 at 0:24
  • 1
    Furthermore: even your accepted answer is using a container to access all your variables. It just happens to be the already existing python var table. Make things easier on yourself and go learn about the keyword 'list', and the list shorthand syntax, e.g. [1, 2, 3] Commented Apr 11, 2015 at 0:30

2 Answers 2

2
def createVars(num, somevalue): l = [{'var'+str(x):somevalue} for x in xrange(num)] return l 
Sign up to request clarification or add additional context in comments.

1 Comment

Since the key is basically just a number anyway, this could probably just be return [somevalue for n in xrange(num)] (or range for Python 3).
1

You can use globals():

>>> for i in xrange(10): ... globals()["x%d" % i] = i ... >>> x0 0 >>> x1 1 >>> x2 2 >>> x10 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x10' is not defined 

16 Comments

This is technically possible, but I'm 99.9% sure that a list, dict, or similar data structure is what the asker would've wanted if he or she knew about them. It's like a question asking how to draw shapes to the console when the desired shapes were just letters and the real question should've been "how do I print?".
This does not even work. I am sick of people telling other people to do this when it doesn't even work. Modifying locals() like that doesn't actually affect a function's local variables.
Well, using globals() is recommended over locals() because locals() does not work inside functions in CPython. According to the API documentation, updates to locals() is never guaranteed to actually update the local symbol table and it is recommended that you don't modify it. See here: docs.python.org/2/library/functions.html#locals
This question is a classic XY problem. It's clear what the OP's problem is, the OP's attempted solution is horrible, and you insist on trying to provide ways to make the OP's attempted solution work without any mention of the best ways to solve the underlying problem. You're making global variables now. That's not a tremendous improvement. Do you want to give the OP the impression that whenever he needs to store N things, he needs N global variables? What happens when he needs a recursive function?
@electron1: The application you were thinking of is still better done with a list. for thing in l: thing.method(). You can even write it on one line if you really want. It would be much harder and more awkward to do that with 2000 named variables.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.