Sure you can; it's called a dictionary:
d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}
I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!
Answer from the wolf on Stack OverflowSure you can; it's called a dictionary:
d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}
I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!
It is really bad idea, but...
for x in range(0, 9):
globals()['string%s' % x] = 'Hello'
and then for example:
print(string3)
will give you:
Hello
However this is bad practice. You should use dictionaries or lists instead, as others propose. Unless, of course, you really wanted to know how to do it, but did not want to use it.
I need to create a large number of variable. Currently what I have is:
var1 = 0 var2 = 0 ... var1000 = 0
Is it possible to create these variables in some sort of loop instead of writing each one?
#Pseudocode
for i in range(1000):
var_i = 0Update:
Thanks a lot for all the comments! The list idea worked!
Videos
You're probably why I would want to do this but I'm just wondering.
Say I have
for num in range (1, 4):
f"created_variable_{num}" = numIt would create 4 variables (if it worked):
created_variable_1 = 1
created_variable_2 = 2
created_variable_3 = 3
But it doesn't. Is there a recommended way to create variables using a loop correctly?
You can use locals() and globals() to dynamically assign variables.
>>> param = range(10)
>>> var = 'abcdefghij'
>>> locals().update({'{}_true'.format(k): v for k, v in zip(var, param)})
>>> c_true
2
>>> f_true
5
It was already discussed here:
Using a string variable as a variable name
In particular, something like this should work:
for k, v in zip(gvalues, params):
exec('%s = %s' % (k, v))
Use a dictionary instead. E.g:
doubles = dict()
for x in range(1, 13):
doubles[x] = x * 2
Or if you absolutely must do this AND ONLY IF YOU FULLY UNDERSTAND WHAT YOU ARE DOING, you can assign to locals() as to a dictionary:
>>> for x in range(1, 13):
... locals()['double_{0}'.format(x)] = x * 2
...
>>> double_3
6
There never, ever should be a reason to do this, though - since you should be using the dictionary instead!
expanding my comment: "use a dict. it is exactly why they were created"
using defaultdict:
>>> from collections import defaultdict
>>> d = defaultdict(int)
using normal dict:
>>> d = {}
the rest:
>>> for x in range(1, 13):
d['double_%02d' % x] = x * 2
>>> for key, value in sorted(d.items()):
print key, value
double_01 2
double_02 4
double_03 6
double_04 8
double_05 10
double_06 12
double_07 14
double_08 16
double_09 18
double_10 20
double_11 22
double_12 24
I think dictionaries are more suitable for this purpose:
>>> name = ['mike', 'john', 'steve']
>>> age = [20, 32, 19]
>>> dic=dict(zip(name, age))
>>> dic['mike']
20
>>> dic['john']
32
But if you still want to create variables on the fly you can use globals()[]:
>>> for x,y in zip(name, age):
globals()[x] = y
>>> mike
20
>>> steve
19
>>> john
32
You can use globals():
globals()[e] = age[index]
Generally, though, you don't want to do that; a dictionary is much more convenient.
people = {
'mike': 20,
'john': 32,
'steve': 19
}