Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the variable name as string from an array of variables?

I have array of dataframes:

a=[df1,df2,df3]

But I need the variable name as a string

b=['df1','df2','df3']

How can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Tooking inspiration from this answer,
you can solve your problem with the following code:

def get_var_name(variable):
     for name in globals():
        if eval(name) is variable:
            return name

a = [df1, df2, df3]

b = [get_var_name(el) for el in a]
# print(b) gives ['df1', 'df2', 'df3']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading