0

I have a large amount of data in variable form:

abc_123 = 5  
def_456 = 7  
ghi_789 = 9 

etc.

I ask the user for various inputs, which builds up a string. For example:

temp = "abc_123"

How do I make it so that temp = abc_123, thus making temp = 5?

1
  • 1
    Don't use variable variables, use a dictionary. Commented Apr 4, 2016 at 20:59

1 Answer 1

1

Make a dictionary and then look up the values by key:

>>> d = {"abc_123": 5, "def_456": 7, "ghi_789": 9}
>>> temp = "abc_123"
>>> d[temp]
5
>>> d.get("invalid", "not found")
'not found'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That worked great. It took some time to rearrange all the data in that format, but it wasn't excruciating enough to write code just to do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.