0

In python I have:

var = "abc"

How can I create a dictionary of name abc using var ?

Ankur Agarwal
  • 21,824
  • 36
  • 127
  • 196

2 Answers2

2

globals() method returns a dictionary that contains all the names of global scope. You can use this to create a variable whose name is stored in another variable. Something like this:

>>> var = 'abc'
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'var': 'abc', '__doc__': None, '__package__': None}
>>> globals()[var] = {}
>>> abc
{}
>>> globals()
{'abc': {}, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'var': 'abc', '__name__': '__main__', '__doc__': None}
>>> abc['p0'] = 2
>>> abc
{'p0': 2}
>>> 
taskinoor
  • 44,735
  • 12
  • 114
  • 137
0

What do you mean? Assign var to a dictionary where 'name' evaluates to 'abc'? In that case,

var = {'name': 'abc'}

Otherwise, I have no idea what you're asking.

robobrobro
  • 300
  • 2
  • 6