By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Setting variable values from dictionary

P: n/a
If I have a dictionary like the following:

{'category' : 2, 'shape', 4}

How can I use this to make
category = 2 and
shape = 4.

I want to be able to do this regardless of the dict values type. So:

{'cateogry' : 2, 'shape' : 'circle'}

will work as well.
Thanks in advance for any help. You guys rock.

--
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sean Berry wrote:
If I have a dictionary like the following:

{'category' : 2, 'shape', 4}

How can I use this to make
category = 2 and
shape = 4.

I want to be able to do this regardless of the dict values type.
So:

{'cateogry' : 2, 'shape' : 'circle'}

will work as well.


locals().update(mydict)

Oops. I just actually read the docs:

locals( )
Update and return a dictionary representing the current
local symbol table. *Warning*: The contents of this
dictionary should not be modified; changes may not affect
the values of local variables used by the interpreter.

[Emphasis added.]

So, perhaps, the following is to be preferred:

for name, value in mydict.items():
exec('%s = %s' % (name, value))

But, what do you want to do about name clashes?

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.