Python Forum
how to display name of a list? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: how to display name of a list? (/thread-35864.html)



how to display name of a list? - 3lnyn0 - Dec-24-2021

Hi! I have a function and I give it as a parameter a list. How can I display the name of the list which I use not the element?

a = [name, street, city]
b = [colour, height, weight]

def add_info(name)
    print(f'Do you you want to add info for {a}?) ---> here I want to print 'a' not the list element
    

add_info(a)



RE: how to display name of a list? - ndc85430 - Dec-24-2021

Sounds like you want to keep your lists together in another collection - use a dictionary, where the keys are the names you want and the values are the lists.


RE: how to display name of a list? - 3lnyn0 - Dec-24-2021

(Dec-24-2021, 12:46 PM)ndc85430 Wrote: Sounds like you want to keep your lists together in another collection - use a dictionary, where the keys are the names you want and the values are the lists.

I have to use lists in this project.


RE: how to display name of a list? - deanhystad - Dec-24-2021

Your program has a variable named "a", but the variable doesn't know this. Why not do this if you want to use the variable name:
def add_info(name)
    print('Do you want to add info for a?'
Or a better name
def add_info(name)
    print('Do you want to add info for address?'



RE: how to display name of a list? - 3lnyn0 - Dec-24-2021

(Dec-24-2021, 01:16 PM)deanhystad Wrote: Your program has a variable named "a", but the variable doesn't know this. Why not do this if you want to use the variable name:
def add_info(name)
    print('Do you want to add info for a?'
Or a better name
def add_info(name)
    print('Do you want to add info for address?'

In the output should be displayed every time the name of the list.
If I use list a print should be "Do you want to add info for a?", and when I use list b print should b "Do you want to add info for b?"
The name should change automatically, depending on which list you use as a parameter


RE: how to display name of a list? - deanhystad - Dec-24-2021

Then you will have to make something that associates a name with a list.
named_list = ["Name", [list items]]

This is ugly, and that is why Python contains constructs other than lists that make it less ugly. Guess you'll learn about those later.

I wonder if your question is wrong. What are you trying to do? I don't mean "How do I print out the name of my list?" I mean what is the problem you are trying to solve? Why are you printing the name of the list?


RE: how to display name of a list? - BashBedlam - Dec-24-2021

Here's one way to go about it:
a = [1,2,3]

def say (var):
	for name in globals():
		if eval (name) == var:
			print(f'Variable name is {name}.')

say(a)



RE: how to display name of a list? - ndc85430 - Dec-25-2021

No, please don't teach people to use eval and things like that :(. They'll end up leaning on it and writing code that is hard to maintain.


RE: how to display name of a list? - jefsummers - Dec-27-2021

Backward tracing from a value to the name is problematic. An object may have multiple variable names pointing to it - and which one do you want? Example:
a = [1,2,3]
b = a
b[1] = 4
print(a)
Output:
[1, 4, 3]
You changed the value in b, but the value also changed in a. Why? Both a and b point to the same object. Back trace from that object to its name? Which one?