1

I am a python beginner and created a short dictionary that links capital letters as keys to 1x3 arrays as the respective values. So something similar to this:

dict = {'A': array([1, 2, 3]), 'B': array([2.1, 3.2, 4]), etc. }

As part of my program I need to assign the key to the value as a function. But instead of writing:

A = array([1, 2, 3])
B = array([2.1, 3.2, 4])
etc.

I want to find a more general, abstract, simpler code. So far I could come up with:

for i in dict:
  i = dict[i]

But this does not quite work and I am unsure why. Sorry that I can not give a better error description here, but I don't have access to my code at the moment. Any help is appreciated! Thanks in advance!

3
  • 3
    Keep data out of your variable names. Why don't you simply use the dictionary directly? (Your code does not work because you'll assign to the name i, not to the name that's in the string pointed to by i.) Commented Mar 13, 2014 at 15:32
  • The main issue you have (other than your variable names) is that you are overwriting i in each iteration. Try changing i = dict[i] to value = dict[i] to see that the loop should then work. Commented Mar 13, 2014 at 15:36
  • More abstraction often does not lead to simpler code. If you want more generic, why would you want variables with specific names created? Using your dictionary's name to access its contents is generic. BTW, dict is the name of a frequently-used, built-in Python data structure, so you might want to avoid that name in your own code to mean something else. Commented Mar 13, 2014 at 17:04

1 Answer 1

1

In your for loop, you're assigning the keys ("A", "B", etc.) to the variable i. So, you're correct that you can access the arrays using dict[i]; however, it doesn't really make sense to assign them to i. My guess is that you want something more like A = dict[i] the first time, and B = dict[i] the second, etc. Chances are you might not need this as the dictionary gives you a nice natural way to access these arrays.

That said, there is a way to get what you want. In your loop, you can say something like:

from numpy import array
d = {'A': array([1, 2, 3]), 'B': array([2.1, 3.2, 4])}
for key, val in d.items():
    vars()[key] = val

print "A =", A
print "B =", B

This is kind of awkward though, because namespaces in python are dictionaries anyway, so you're just taking your dictionary and putting it in the dictionary that vars() gives you.

Also, as an aside, it's sort of messy (but still valid) to call your dictionary 'dict' since dict is the builtin name for the dictionary class. So you can use dict(a=0, b=1, ...) to create a dictionary, but not if you override that name with a local variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, as the article says, there are subtle edge cases where one needs this dynamic locals() access. But hopefully after seeing how it's done, the OP will agree that just working with the dict is more natural and clear programming. It's not really possible to show a better approach since the OP doesn't say what they're trying to do with these arrays.
I didn't down-vote your answer as it does show a way to create the variables. All I did was comment that I thought it could be better -- and how it could have been, including saying don't do it. BTW, using vars() like that inside a function or method might not do what is desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.