Create dictionary where keys are variable names
<p>I quite regularly want to a dictionary where the key names are the variable names. For example if i have the variable <code>a</code> and <code>b</code> then to have a dictionary <code>{"a":a, "b":b}</code>  (typically to return data at the end of a function).</p>

<p>Is there any (ideally built in) ways in python of automatically doing this? i.e to have a function such that <code>create_dictionary(a,b)</code> returns <code>{"a":a, "b":b}</code></p>

<p>Have you tried something like:</p>

<pre><code>a, b, c, d = 1, 2, 3, 4
dt = {k:v for k, v in locals().items() if not k.startswith('__')}
print(dt)
{'a': 1, 'd': 4, 'b': 2, 'c': 3}
</code></pre>

python python-3.x