0

I'm trying to do something like:

a = input("Enter the name of your list")
"{n}".format(n = a) = []

Which is obviously doesn't work. Is there any way to do this?

2 Answers2

4

One way to have a variable with a user-supplied name is to create that variable as an item within a dictionary.

the_dict = {}
a = input("Enter the name of your list")
the_dict[a] = []
John Gordon
  • 29,573
  • 7
  • 33
  • 58
2

Use exec and eval:

a = input("Enter the name of your list")
exec(str(a)+"=[]")
print(eval(a))

This does exactly what you want, while this is not recommended, for better solution please refer to John's answer.

Kevin Fang
  • 1,966
  • 2
  • 16
  • 31