0

In python I have:

var = "abc"

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

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208

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
  • 45,586
  • 12
  • 116
  • 142
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
  • 320
  • 2
  • 6