I want to accomplish the following:
vname = [ 'name1', 'name2']
for i in len(vname):
vname[i] = {}
vname[i]['key1'] = 4*i
I expect to create two dictionaries name1 and name2 such that
name1['key1'] = 0
name2['key1'] = 4
I'd use a nested dictionary instead, generating values with the help of enumerate():
>>> {key: {'key1': index * 4} for index, key in enumerate(vname)}
{'name2': {'key1': 4}, 'name1': {'key1': 0}}
In order to dynamically create variables of a certain name, you could add them to the global namespace:
>>> vname = [ 'name1', 'name2']
>>> for i in range(len(vname)):
... globals()[vname[i]] = {}
... globals()[vname[i]]['key1'] = 4*i
...
>>> name1
{'key1': 0}
>>> name2
{'key1': 4}
However, I have never encountered a situation where this was useful or necessary. If you are trying to sth. like this, you are very likely better off with a different (and better behaved) data structure like the nested dict suggested by @alexce.
vname[i] = {}you replace the string with a dictionary so the variablesname1andname2never exist, also I assume you mean to use4*iinstead of4*1since the latter is just4.