Python: How to extract variable name of a dictionary entry?


I'm wondering how I would go about finding the variable name of a dictionary element:

For example:


    >>>print dict2
    {'nth_dict_item': {'0001': '0002'}}
    >>>print dict2['nth_dict_item']
    {'001': '002'}

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.

If I compare the output of id(dict1) to id(dict2['nth_dict_item']) I can find they are the same.

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?

I'm sure I'm just overlooking a function that would make my life easy but I'm pretty new to Python :)

Any help is appreciated, thanks!


Update: Here's why I wanted this to work:

I'm trying to make an app that uses a dictionary kinda like a database. I wanted the functionality of this psuedocode to function:




SomeUniqueIdentifier# would be a unique value that I'm using as a database key/unqiueID to look up entries.

I want to be able to update the "comments" field of FooBar1.avi by:

WhichDict= dict_database[SomeUniqueIdentifier1]

instead of having to do:


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!.

By : Brandon K


Use Guppy.

from guppy import hpy

0: h.Root.i0_modules['__main__'].__dict__['dict1']
By : vartec

You need to rethink your design.

A quick hack would be to actually put the variable name in ...

dict2['item_n'] = 'dict1'

or maybe use a tuple ..

dict2['item_n'] = ('dict1', dict1)

There's a function called locals() which gives a dictionary of local variable names, maybe you can consider it when you rethink about your design.

Here, have a look at how locals() works:

>>> x = 10
>>> y = 20
>>> c = "kmf"
>>> olk = 12
>>> km = (1,6,"r",x)
>>> from pprint import pprint
>>> pprint( locals() )
{'__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__name__': '__main__',
 'c': 'kmf',
 'km': (1, 6, 'r', 10),
 'olk': 12,
 'pprint': <function pprint at 0x016A0EB0>,
 'x': 10,
 'y': 20}


I want to be able to update the "comments" field of FooBar1.avi by:

WhichDict= dict_database[SomeUniqueIdentifier1]

Well, this is trivial

dict_databse[identifier][2] = the_new_comment

By : hasen

Right now, for each movie, you're doing:

dict_n = {}
dict_n["000n"] = [movie_duration,movie_type,comments]

with only one entry per dict. Then you're storing multiple movie dicts in a central one:

dict_database = {}
dict_database["0001"] = dict_1
dict_database["0002"] = dict_2
dict_database["0003"] = dict_3
dict_database["000n"] = dict_n

Edit: This is probably what you had in mind.

For each movie, there's a dict going from property names to values.

movie_n = {}
movie_n["movie_duration"] = 333
movie_n["movie_type"] = "Fantasy"
movie_n["comments"] = ""

Then movies["any_file.avi"] gives you that dict. To add the movie to the directory and then add a comment, you enter:

movies["any_file.avi"] = movie_n
movies["any_file.avi"]["comments"] = "new comment"

If you have to choose between the number of that movie and the filename, I'd go with the filename because it's simply more relevant when you're actually trying to access a movie. Plus, if necessary you can enumerate your movies (albeit out of order) through movies.keys() or something similar. If you want to use both a number and the filename, add the filename as another key for each movie.

movie_n["filename"] = "any_file.avi"


movies["000n"]["filename"] = "any_file.avi"

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