+3 votes
in Programming Languages by (71.8k points)

I have multiple variables. How can I create a dictionary using those variables such as keys are variable names and values of those variables are values of the dictionary?

E.g.

From

a=10

b=20

c=30

to

{'a': 10, 'b': 20, 'c': 30}

1 Answer

+3 votes
by (348k points)
selected by
 
Best answer

You can apply dict() to those variables to create a dictionary.

Here is an example:

>>> a=10
>>> b=20
>>> c=30
>>> dict(a=10, b=20, c=30)
{'a': 10, 'b': 20, 'c': 30}
 


...