title: Python: access variable from string
body: <p>This is probably something very basic and simple, and I'm probably just googling for the wrong terms, but hopefully someone here can help me. (I'm still a beginner to programming, which is probably obvious from this question.)</p>
  
  <p>I'm looking for a way to access variables from strings.</p>
  
  <p>Like this:</p>
  
  <pre><code>A1 = {}
  B1 = {}
  C1 = {}
  
  mylist = ["A","B","C"]
  
  for blurb in mylist:
      blurb1.foo()
  </code></pre>
  
  <p>Right now I'm using if/elif-constructions in such cases:</p>
  
  <pre><code>if blurb == "A":
      A1.foo()
  elif blurb == "B":
      B1.foo()
  elif blurb == "C":
      C1.foo()
  </code></pre>
  
  <p>That works, but surely there's a more elegant way of doing it?</p>
  
  <p>Thanks for any help!
  Lastalda</p>
  
  <hr>
  
  <p>Edit2: trying to clarify again (sorry for not being very coherent before, it's hard for me to get this across):</p>
  
  <p><strong>I would like to create and access objects from strings.</strong></p>
  
  <p>So if i have a function that returns a string, I want to create e.g. a list using this string as the list name, and then do stuff with that list. </p>
  
  <pre><code>x = foo() # foo returns a string, e.g. "A"
  # create list named whatever the value of x is
  # in this case: 
  A = []
  </code></pre>
  
  <p>Isn't there anything more elegant than preparing lists for all possible outcomes of x and using long elif constructions or dictionaries? Do I have to use eval() in that case? (I'm hesitant to use eval(), as it's considered dangerous.)</p>
  
  <p>I hope it's finally clear now.
  Any help still appreciated!</p>