suppose I have lists of strings like this
list1 = ["x","y","z"]
so how can create empty dictionaries like x = {}, y = {} and z = {} by iteration
Following method does nothing:
for i in list1:
i = dict()
As recommended do not dynamiclly create variable from strings
This said, you may store this in a dict to store, then associate an empty dict for each key
result = {}
for idx in list1:
result[idx] = {}
print(result)
# {'x': {}, 'y': {}, 'z': {}}
Explicit is better than implicit. It's about code generation where things can go much further than you expect. Python is not good option for it. Look at Lisp. But it's strongly opinion-based statement and not for this site, unfortunately.You can use the built-in exec function.
For example, exec("x=3") creates the x variable, assigning to it the value 3.
Your specific example can be solved like this:
for var in list1:
exec(var + "={}")
var as dumpDatabaseWithPasswords(); x. For homework and five-lines scripts can be okay, but 99% cases it doesn't necessary.This dynamic creation of variable names is not advised most of the time. Check Creating dynamically named variables from user input.
I am not sure what you are attempting to do but would this is a possible approach.
list1 = ["x","y","z"]
d = {}
for i in list1:
d[i] = {}
You would get a dict with an empty dict inside for each of your strings.
print(x) # {}or indexes to access the dictprint(somename['x']) # {}?