Python Forum
how to display name of a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to display name of a list?
#1
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)
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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?'
Reply
#5
(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
Reply
#6
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?
ndc85430 likes this post
Reply
#7
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)
ibreeden and 3lnyn0 like this post
Reply
#8
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.
buran likes this post
Reply
#9
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?
BashBedlam likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020