0

Hello I was wondering if there was a way of calling a variable if I had it's name as a string. For example I have a list of names

    variables=["a","b","c"]
    a=2
    b=3
    c=4

I would like to call the variables using the strings stored in the list which storing their values in the list allowing me in essence to have a list of variables. e.g. d=a[0] I would like d to equal the value of the variable a not store the string value of a Is there a way to do this?

Archie Godfrey
  • 143
  • 3
  • 10
  • 4
    Yes, but there is probably a better way to do whatever you are actually trying to do. What are you *actually* trying to do? – khelwood Nov 01 '16 at 15:10
  • I want to call the variable from it's string value rather than store the variables value in the list. – Archie Godfrey Nov 01 '16 at 15:10
  • 2
    Maybe you want a dictionary from what I understand? – MooingRawr Nov 01 '16 at 15:11
  • 1
    Using eval ? a=2, then using eval(variables[0]) or eval('a') both should evaluate to 2 – Skycc Nov 01 '16 at 15:14
  • that's just what I was looking for thanks @Skycc – Archie Godfrey Nov 01 '16 at 15:16
  • @MartijnPieters I wasn't asking how to create a variable number of variables, I was asking how to call a variable from having the name of the variable stored in a string form – Archie Godfrey Nov 01 '16 at 15:24
  • @thestarchyninja: the answer is *exactly the same*, wether you want to access variables or create them. – Martijn Pieters Nov 01 '16 at 15:25
  • oh sorry what is the right thing to do in this situation @MartijnPieters should I delete the post or should I just leave it – Archie Godfrey Nov 01 '16 at 15:27
  • Normally I'd say that duplicates have a utility in that they exist to be sign-posts to the canonical post (anyone using a Google search may find your post first, then get redirected automatically to the other post), but there are [already nearly 200 such posts](https://stackoverflow.com/questions/linked/1373164?lq=1), so it's up to you really. – Martijn Pieters Nov 01 '16 at 16:02

0 Answers0