-1

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()
2
  • 2
    There is a debate in the answer : do you want new variables, like print(x) # {} or indexes to access the dict print(somename['x']) # {} ? Commented Dec 27, 2019 at 10:59
  • Could you answer us ? Because some answers are upvoted, one is accepted, and all of them gives different answers Commented Dec 27, 2019 at 17:44

5 Answers 5

3

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': {}}
Sign up to request clarification or add additional context in comments.

4 Comments

This will not create variables. It will give dictionary which is wrong as per his expected output. He wants variables with dict type.
@VaibhavJadhav I know, but as I wrote it's not recommended so I did not propose this idea, a dict that handle this is better
The question is what does the author expect it for. Offered way is better to solve the most problems. 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.
@mrEvgenX how can create empty dictionaries is not very explicit that he waits for new variable, let's wait for the OP to give further details
1

Check out the following code:

list1 = ["x","y","z"]
for i in list1:
    globals()[i] = dict()

This will give you:

x = {}
y = {}
z = {}

To check the output and its types you can do the following:

print(x)
print(type(x))
print(y)
print(type(y))
print(z)
print(type(z))

Comments

1

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 + "={}")

2 Comments

Very dangerous, on my opinion. What if it will be the part of web-application and user can send a string var as dumpDatabaseWithPasswords(); x. For homework and five-lines scripts can be okay, but 99% cases it doesn't necessary.
@mrEvgenX Totally agree. It's not a good idea to dynamically create variables most of the times. I was simply trying to satisfy the specific needs of the user: he asked for a way to create new variables (I hope he won't use my answer as part of a web-application)
0

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.

2 Comments

This will not create variables. It will give dictionary which is wrong as per his expected output. He wants variables with dict type.
@VaibhavJadhav i know this does not return variables but maybe the question author does not really need variables and this could be enough.
-1

Here is one solution :

for i in list1:
    locals()[i] = dict()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.