-3

Suppose user enters hello

I want to create empty dictionary hello = {} which will be later used as required.

sP_
  • 1,738
  • 2
  • 15
  • 29

1 Answers1

1

If you want to create a dictionary whose name is input by the user, you can use something like this:

while True:
    dictName = raw_input('Name: ') # use input on Python 3
    if not dictName:
        break

    globals()[dictName] = {}

print test1
print test2

The code above will ask for a name and inject a new dict into globals, so you can reference that later in the code (ie. print it). It doesn't use eval, which is unsafe.

Example usage:

Name: test1
Name: test2
Name:
{}
{}
Community
  • 1
  • 1
Nacib Neme
  • 859
  • 1
  • 17
  • 28