title: Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)?
body: <p>I have a list of variable names, like this:</p>
  
  <pre><code>['foo', 'bar', 'baz']
  </code></pre>
  
  <p>(I originally asked how I convert a list of variables.  See Greg Hewgill's answer below.)</p>
  
  <p>How do I convert this to a dictionary where the keys are the variable names (as strings) and the values are the values of the variables?</p>
  
  <pre><code>{'foo': foo, 'bar': bar, 'baz': baz}
  </code></pre>
  
  <p>Now that I'm re-asking the question, I came up with:</p>
  
  <pre><code>d = {}
  for name in list_of_variable_names:
      d[name] = eval(name)
  </code></pre>
  
  <p>Can that be improved upon?</p>
  
  <p><strong>Update</strong>, responding to the question (in a comment) of why I'd want to do this:</p>
  
  <p>I often find myself using the % operator to strings with a dictionary of names and values to interpolate.  Often the names in the string is just the names of local variables.  So (with the answer below) I can do something like this:</p>
  
  <pre><code>message = '''Name: %(name)s
  ZIP: %(zip)s
  
  Dear %(name)s,
  ...''' % dict((x, locals()[x]) for x in ['name', 'zip'])
  </code></pre>