Linked Questions
49 questions linked to/from Simpler way to create dictionary of separate variables?
10 votes
1 answer
33k views
Getting the name of a list as a string in Python [duplicate]
After a bit of research I found these questions (1, 2) about variables. Similarly, I would like to know if it is possible to get the name of a list as a string in Python, for example: for a list lst =...
0 votes
2 answers
15k views
Print variable name in python [duplicate]
Here bkd_train, bkd_test are dataframe. I wanted to print dataframe name along with it's shape. for data in [bkd_train, bkd_test]: print("{} : {}".format(?, data.shape)) If i am using "data" ...
-2 votes
2 answers
3k views
Converting variable name into a string then printing it? Python [duplicate]
Im wondering if there is a way to take a variable's name and convert it into a string to print it? for example: myVariable = 1 i want to convert "myVariable" like the name of the variable itself not ...
1 vote
0 answers
1k views
How to get variable name from list? [duplicate]
I put integers into list and then sort it: a,b,c,d,e,f = 3,6,2,8,1,2 list1=[a,b,c,d,e,f] list5.sort() Now I want to print the sorted list as string, but to see the variables, like: efcabd I can't ...
0 votes
0 answers
331 views
get variable name of instance [duplicate]
Can a Python instance get the name of the variable which is used to access the object? This example code shows what I need: foo=MyClass() foo.get_name() --> 'foo' bar=foo bar.get_name() --> '...
2 votes
1 answer
216 views
How to pprint and automatically include the the variable name [duplicate]
For Python 2.7 I have a little utility for debugging: def printvar(label, var): print "%s:\n%s" % (label, pformat(var)) Often I call it like printvar("foo", foo). Is it possible to simplify that ...
0 votes
0 answers
177 views
How to get a dataframe name as a string in python [duplicate]
I have a pandas DataFrame named 'data_by_month' Here is a dummy data: data = {'date' : ['1', '2','3'], 'value1' : ['a', 'b' ,'c'], 'value2' : ['12','24','4']} data_by_month = pd....
-5 votes
5 answers
275 views
How to print varible name in python [duplicate]
I have a variable fruit = 13, when I print(fruit) I get 13. How do I print out the variable name so I get 'fruit' to print out? I don't want to have to use print('fruit') because I will be using ...
0 votes
1 answer
88 views
String from Object Python [duplicate]
I am working on a project that uses XML to create a Tkinter GUI and need to know how to convert an object name to a string. For example: # In the actual program, the value of widget variables is set ...
0 votes
0 answers
84 views
convert an object to string in python [duplicate]
I am working with mongodb in python and want to define some functions. One function needs a collection object as well as its name represented as string. However for the user its the same name. The ...
0 votes
0 answers
55 views
How to add a list to a dictionary? [duplicate]
Is there a straight forward way to add a list to a dictionary? For example a = [1,2,3] d = dict(a) I have seen a way (I do not remember exactly how) that add a list to a dictionary making the name ...
0 votes
0 answers
32 views
string name of variable(object) [duplicate]
in C# nameof(variable) returns "variable" cant i do something like this in python? class ClassName: someVariable = "im in love with a coco" if its a class, i can use ClassName.__name__ and this ...
118 votes
8 answers
313k views
Get the name of a pandas DataFrame
How do I get the name of a DataFrame and print it as a string? Example: boston (var name assigned to a csv file) import pandas as pd boston = pd.read_csv('boston.csv') print('The winner is team A ...
70 votes
14 answers
55k views
How to print original variable's name in Python after it was returned from a function?
I have enum and use the variables like myEnum.SomeNameA, myEnum.SomeNameB, etc. When I return one of these variables from a function, can I print their names (such as myEnum.SomeNameA) instead of the ...
16 votes
7 answers
98k views
Get name of dictionary
I find myself needing to iterate over a list made of dictionaries and I need, for every iteration, the name of which dictionary I'm iterating on. Here's an MRE (Minimal Reproducible Example). Contents ...