-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()
immutable
  • 2,174
  • 3
  • 20
  • 27
Saurabh
  • 1,592
  • 2
  • 14
  • 30
  • 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']) # {}` ? – azro Dec 27 '19 at 10:59
  • Could you answer us ? Because some answers are upvoted, one is accepted, and all of them gives different answers – azro Dec 27 '19 at 17:44

5 Answers5

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': {}}
azro
  • 53,056
  • 7
  • 34
  • 70
  • This will not create variables. It will give dictionary which is wrong as per his expected output. He wants variables with dict type. – Vaibhav Jadhav Dec 27 '19 at 10:48
  • 1
    @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 – azro Dec 27 '19 at 10:51
  • 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 Dec 27 '19 at 10:57
  • 1
    @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 – azro Dec 27 '19 at 10:58
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))
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
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 + "={}")
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • 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 Dec 27 '19 at 11:01
  • 1
    @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) – Riccardo Bucco Dec 27 '19 at 11:06
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.

  • This will not create variables. It will give dictionary which is wrong as per his expected output. He wants variables with dict type. – Vaibhav Jadhav Dec 27 '19 at 10:50
  • 1
    @VaibhavJadhav i know this does not return variables but maybe the question author does not really need variables and this could be enough. – André Cascais Dec 27 '19 at 10:56
-1

Here is one solution :

for i in list1:
    locals()[i] = dict()
Philippe
  • 20,025
  • 2
  • 23
  • 32