Names of objects in a list?
<p>I'm trying to iteratively write dictionaries to file, but am having issues creating the unique filenames for each dict.</p>

<pre><code>def variable_to_value(value):
    for n, v in globals().items():
        if v == value:
            return n
    else: 
        return None

a = {'a': [1,2,3]}
b = {'b': [4,5,6]}
c = {'c': [7,8,9]}

for obj in [a, b, c]:
    name = variable_to_value(obj)
    print(name)
</code></pre>

<p>This prints:</p>

<pre><code>a
obj
obj
</code></pre>

<p>How can I access the name of the original object itself instead of <code>obj</code>? </p>

<p>So you want to find the name of any object available in the <code>globals()</code>?</p>

<p>Inside the <code>for</code> loop, <code>globals() dict</code> is being mutated, adding <code>obj</code> in its namespace. So on your second pass, you have two references to the same object (originally only referenced by the name 'a').</p>

<p>Danger of using <code>globals()</code>, I suppose. </p>

python list introspection