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?

martineau
  • 119,623
  • 25
  • 170
  • 301
d2907
  • 798
  • 3
  • 15
  • 45
  • 1
    What would be *the name of the list*? – Michael Szczesny Oct 25 '21 at 21:08
  • 2
    No, that's not possible. See https://stackoverflow.com/questions/1538342/how-can-i-get-the-name-of-an-object – mkrieger1 Oct 25 '21 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 – bherbruck Oct 25 '21 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? – Peter Wood Oct 25 '21 at 21:11
  • @mkrieger1 - Unfortunately there are even [packages](https://pypi.org/project/varname) for this. – Michael Szczesny Oct 25 '21 at 21:11
  • @MichaelSzczesny How on earth would you maintain code written like with that package? – Peter Wood Oct 25 '21 at 21:13
  • 2
    You don't understand how names and values work in Python. See [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html). – martineau Oct 25 '21 at 22:34
  • Thanks all of you for your help. As I said I'm new with python. I did not about dictionary. It is very useful. This was a requirement from client and wanted to be sure about this before answering. – d2907 Oct 26 '21 at 18:00

4 Answers4

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"]
Odhian
  • 351
  • 5
  • 14
  • Thanks Odhian. As I said, I'm new in Python, I was not aware about this structure. Very useful – d2907 Oct 26 '21 at 17:55
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?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 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. – d2907 Oct 26 '21 at 17:56
  • 1
    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?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Peter Wood Oct 26 '21 at 19:44
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())
Joe
  • 65
  • 8
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']
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
  • Thanks Prashant. As I said, I'm new in Python, I was not aware about this structure. Very useful – d2907 Oct 26 '21 at 17:54