1

I'm wondering if something like this is possible. Let's suppose this snippet of code:

area = ["A","B","C"] level = ["L1","L2","L3"] sector = [area, level] print(sector) print(sector[1]) 

Output:

  • Print 1: [['A', 'B', 'C'], ['L1', 'L2', 'L3']]
  • Print 2: ['L1', 'L2', 'L3']

The first print is OK for me. It shows the lists and their elements.

However, for the second print I would like to have the name of the list instead of its elements. In this case level

Is that possible?

8
  • 1
    What would be the name of the list? Commented Oct 25, 2021 at 21:08
  • 2
    No, that's not possible. See stackoverflow.com/questions/1538342/… Commented Oct 25, 2021 at 21:08
  • I don't think this is really useful to even get in the first place, If you want to map some variables to a "name" I would look into using dictionaries Commented Oct 25, 2021 at 21:09
  • 1
    A name is just a name for an object. The object doesn't know and can have multiple names. The list contains objects which may or may not also have names elsewhere. Why do you need this? Commented Oct 25, 2021 at 21:11
  • 2
    You don't understand how names and values work in Python. See Facts and myths about Python names and values. Commented Oct 25, 2021 at 22:34

4 Answers 4

2

What you can do though, is use a dictionnary:

di = {"area": ["A", "B", "C"], "level": ["L1", "L2", "L3"]} di["area"] 

Output :

["A", "B", "C"] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Odhian. As I said, I'm new in Python, I was not aware about this structure. Very useful
2

You could compare the id:

for sec in sector: if id(sec) == id(area): print('area') elif id(sec) == id(level): print('level') 

etc.

However, this is a dubious way to go. Why do you need this?

2 Comments

It was a requirement from the client. But given that my knowledge is limited right now I want to be sure before answering. Thanks for your help.
I would question why the client needs this. It's not easily supported in Python, and really suggests it's ill-conceived. See What is the XY problem?
1

Make it into a Dictionary of Lists instead of a List of Lists:

area = ["A","B","C"] level = ["L1","L2","L3"] sectors = { "area": area, "level": level } print(sectors["level"]) print(sectors["area"]) """ some other useful dict methods below """ print(sectors.keys()) print(sectors.values()) print(sectors.items()) 

1 Comment

Thanks Joe. Your example was very useful. I've learn something new about Python.
0

You can use a dictionary for your usecase.

You can make the name of the variables as key and lists as values.

Try this:

area = ["A","B","C"] level = ["L1","L2","L3"] sector = {"area":area, "level":level} print(list(sector.values())) print(list(sector.keys())) 

Outputs:

[['A', 'B', 'C'], ['L1', 'L2', 'L3']] ['area', 'level'] 

1 Comment

Thanks Prashant. As I said, I'm new in Python, I was not aware about this structure. Very useful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.