0

I would like to convert each item of a list of strings into an empty list object with the string as list name.

Input:

list_items = ["string1","string2"]

Output: 2 empty lists called "string1" and "string2

 print(string1) -> []
 print(string2) -> []

Thank you!

olive
  • 179
  • 1
  • 11

1 Answers1

0

Use local()

list_items = ["string1","string2"]
for l in list_items:
   locals()[l] = []

print(string1)
print(string2)

NB: there are strong arguments against this method but it will do exactly what you are looking for.

  • See the duplicate for recommended ways to do what the OP wants, but note that [the documentation for locals()](https://docs.python.org/3/library/functions.html#locals) explicitely states not to modify it: "Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter." – Thierry Lathuille Feb 15 '22 at 16:08