Set variables based on dictionary

P

Peter Otten

Joan said:
How to set local variables based on dictionary contents?
.... for k, v in d.iteritems(): exec "%s = v" % k
.... return locals()
....{'a': 42, 'k': 'b', 'b': 'yadda', 'd': {'a': 42, 'b': 'yadda'}, 'v':
'yadda'}warning: this can execute arbitrary code
{'a': 42, 'k': "print 'warning: this can execute arbitrary code'; a", 'd':
{"print 'warning: this can execute arbitrary code'; a": 42}, 'v': 42}

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,515
Latest member
Harvey7327

Latest Threads

Top