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)?


I have a list of variable names, like this:

['foo', 'bar', 'baz']

(I originally asked how I convert a list of variables. See Greg Hewgill's answer below.)

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?

{'foo': foo, 'bar': bar, 'baz': baz}

Now that I'm re-asking the question, I came up with:

d = {}
for name in list_of_variable_names:
    d[name] = eval(name)

Can that be improved upon?

Update, responding to the question (in a comment) of why I'd want to do this:

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:

message = '''Name: %(name)s
ZIP: %(zip)s

Dear %(name)s,
...''' % dict((x, locals()[x]) for x in ['name', 'zip'])


You can use list or generator comprehensions to build a list of key, value tuples used to directly instantiate a dict. The best way is below:

dict((name, eval(name)) for name in list_of_variable_names)

In addition, if you know, for example, that the variables exist in the local symbol table you can save yourself from the dangerous eval by looking the variable directly from locals:

dict((name, locals()[name]) for name in list_of_variable_names)

After your final update, I think the answer below is really what you want. If you're just using this for string expansion with strings that you control, just pass locals() directly to the string expansion and it will cherry-pick out the desired values

If, however, these strings could ever come from an outside source (e.g. translation files), than it's a good idea to filter locals()

Forget filtering locals()! The dictionary you give to the formatting string is allowed to contain unused keys:

>>> name = 'foo'
>>> zip = 123
>>> unused = 'whoops!'
>>> locals()
{'name': 'foo', 'zip': 123, ... 'unused': 'whoops!', ...}
>>> '%(name)s %(zip)i' % locals()
'foo 123'
By : hop

Your original list [foo, bar, baz] doesn't contain the variable names, it just contains elements that refer to the same values as the variables you listed. This is because you can have two different variable names that refer to the same value.

So, the list by itself doesn't contain information about what other names refer to the objects. The first element in your array has the name foo but it also has the name a[0] (assuming your array is called a). After executing the following code, quux also refers to the same object:

quux = a[0]

Update: You're right that you can use eval() for that, but its use is generally discouraged. Python provides a special member named __dict__ that contains the symbol table for the current module. So you can:

import __main__
d = dict((x, __main__.__dict__[x]) for x in list_of_variable_names)

Having to import __main__ when your code is in the unnamed main module is a quirk of Python.

This video can help you solving your question :)
By: admin