I'm doing a series of exercises and in one I would find it useful to include the name of a list that I iterate over in a for loop.
The loop would look like this:
datosReales = [mpg_float,
cylinders_float,
displacement_float,
horsepower_float,
weight_float,
acceleration_float,
year_float,
origin_float]
for item in datosReales: #DatosReales es una lista de listas.
print("La lista {} contiene {} elementos(datos).".format("item",len(item)))
Where I write "item", I should write some function so that when the text is printed on the screen, it does not show me the content of the list, but simply its name.
EXAMPLE:
The mpg_float list contains n elements(data).
Personally, I think that searching for the variable where an object is stored is not the most convenient, since it entails the following problems:
The object could have multiple variables referencing it. Which one is correct then? How do you know that said variable was used to generate your list?
You have to look at everything that can reference the object: local variables, global variables, non-local variables, active generators, classes, containers, etc.
I think the best option is to use a dictionary or a class instead. That way you will restrict the search to a specific namespace.
Example with dictionary:
Example with classes:
The advantage of this second way is that you can use attributes to access each value instead of brackets and quotes like in dictionaries.
Note: Dictionaries guarantee insertion order from to 3.7. If you have an older version and want to have ordered dictionaries, I recommend using the collections module's OrderedDict, which maintains the order regardless of version.
Good day,
Looking for information about it I found that you can use the
varname
. To do so, you just have to install it:And this would be an example to get the name of any variable
This prints
prueba
.There is another way to do it, but it is a bit more complex.
If we want to see all the global variables created in a program, we can do it as follows:
This will print a dictionary with all the global variables and symbols of the current program, for example, when using the following code from a cell of
Jupyter Lab
(But you would have to adapt it in case the variables are attributes of a function or class since they would be inside local variables of that function or class).Returns the following:
As we can see, at the end is our variable
prueba
. Being a dictionary, we can only obtain the keys (which would be the names of the variables) when usingglobals().keys()
.Now, to get the name of some variable we can look for that variable in the keys of our dictionary and when it matches then return that name:
Note: The function
get_var_name()
contains the special functioneval()
that interprets and executes what it contains literally as codePython
for what could be a risk or vulnerability in the program, for example, the following line:print
peligro
. But for example you could run other scripts, etc...Since you did not put the complete code, I converted
string
all the variables inside your list to exemplify the use of the function:This prints:
Edition
Thanks to the comment from @DanteS. I modified the function to remove the
eval
and remove the risk: