Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: How to extract variable name of a dictionary entry?
    primarykey
    data
    text
    <p>I'm wondering how I would go about finding the variable name of a dictionary element:</p> <p>For example:</p> <pre><code> &gt;&gt;&gt;dict1={} &gt;&gt;&gt;dict2={} &gt;&gt;&gt;dict1['0001']='0002' &gt;&gt;&gt;dict2['nth_dict_item']=dict1 &gt;&gt;&gt;print dict2 {'nth_dict_item': {'0001': '0002'}} &gt;&gt;&gt;print dict2['nth_dict_item'] {'001': '002'} </code></pre> <p>How can I go about making it tell me that dict2['nth_dict_item'] is or is referencing "dict1" ? I want the name of the data structure it's referencing and not the data itself.</p> <p>If I compare the output of id(dict1) to id(dict2['nth_dict_item']) I can find they are the same.</p> <p>But how can I turn that id back into a variable name? Is there a more direct/cleaner method of getting the information that I want to know?</p> <p>I'm sure I'm just overlooking a function that would make my life easy but I'm pretty new to Python :)</p> <p>Any help is appreciated, thanks!</p> <h3>Related</h3> <ul> <li><a href="https://stackoverflow.com/questions/544919/python-can-i-print-original-var-name">python, can i print original var name?</a></li> <li><a href="https://stackoverflow.com/questions/592746/how-can-you-print-a-variable-name-in-python">How can you print a variable name in python?</a></li> </ul> <hr> <p>Update: Here's why I wanted this to work:</p> <p>I'm trying to make an app that uses a dictionary kinda like a database. I wanted the functionality of this psuedocode to function:</p> <pre><code>dict_1={} dict_2={} dict_3={} dict_1["FooBar1.avi"]=[movie_duration,movie_type,comments] dict_2["FooBar2.avi"]=[movie_duration,movie_type,comments] dict_3["FooBar3.avi"]=[movie_duration,movie_type,comments] dict_database[SomeUniqueIdentifier1]=dict_1 dict_database[SomeUniqueIdentifier2]=dict_2 dict_database[SomeUniqueIdentifier3]=dict_3 </code></pre> <p>SomeUniqueIdentifier# would be a unique value that I'm using as a database key/unqiueID to look up entries.</p> <p>I want to be able to update the "comments" field of FooBar1.avi by:</p> <pre><code>WhichDict= dict_database[SomeUniqueIdentifier1] WhichDict[WhichDict.keys()[0]][2]='newcomment' </code></pre> <p>instead of having to do:</p> <pre><code>dict_database['SomeUniqueIdentifier1'][dict_database['SomeUniqueIdentifier1'].keys()[0]][2]='newcomment' </code></pre> <hr> <p>Thanks everyone. I now understand I was misunderstanding a LOT of basics (total brain fart). Will go back and fix the design. Thanks to you all!.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload