How to find the name of a list in a list of lists instead of its value?
Solution 1
You want to use a dictionary instead of a list. Where a list is just an enumeration of values, a dictionary is a key-value data structure.
This is how your example would look as a dictionary literal:
{
"x": ["a", "b"],
"y": ["c", "d"],
}
Unlike a list, a dictionary does not preserve the order of elements however! If you had an application where you needed to preserve order, you can use an OrderedDict. UPDATE: This paragraph is no longer true as of Python 3.7.
Solution 2
Having the name of variable as your output is not possible, unless you change some strategies in your code, for e.g:
x = ["a", "b"]
y = ["c", "d"]
z = {'x': x, 'y': y}
using dictionaries might be better suited to your problem. You can then search for value ["a", "b"] and reach the name 'x'.
Solution 3
a list is a mutable datatype in python. so with
x = ["a", "b"]
python will allocate some memory and store the adress in x. so x is internally something like:
<List with data at 0x123456789>
when you make z, python allocates some memory and stores the adress in z then, it will store the adresses of x and y in the memory of z. so z is internally:
[<List with data at 0x123456789>, <List with data at 0x987654321>]
now you can see that there is no information of the name of the variable which also stores the adress 0x123456789
Summary: you can't get the variable name of a list object.
Solution 4
Maybe just put the list name at the start of each list and instead use z[0][0] to get the name?
Related videos on Youtube
NewHere
Updated on June 04, 2022Comments
-
NewHere almost 2 years
I am trying to find the name of a list in a list of lists (is there a specific name for "list of lists"?).
For example I have the following lists:
x= ["a","b"] y= ["c","d"] z= [x,y]
If I now want to know what is the name of the first list in list "z" I would try like that:
print(z[0])
But instead of "x" I get the value of x ("a","b")
- How can I get the output "x"?
-
sehigle over 5 yearsYou can't. Normally variable names are just for you. If you want that, you should save them in a string.
-
Benjamin Engwall over 5 yearsTo answer your other question, one would often refer to a "list of lists" as a "nested list".
-
bruno desthuilliers over 5 yearsThis is impossible - names know what objects they point to, objects don't know which names point to them. FWIW, this list could have a dozen names pointing to it in different scopes, so even if an object knew which names were pointing to it, it still wouldn't be of any use. Also, consider the case where you rebind
x
after creatingz
, what should be "the name" ofz[0]
then ? -
bruno desthuilliers over 5 yearsNB: you definitly want to read this about how python variables work: nedbatchelder.com/text/names.html
-
NewHere over 5 years@bruno desthuilliers Thank you for the link :)
-
roganjosh over 5 yearsPrior to Python 3.6 a dictionary does not preserve order. In 3.6 it's an implementation detail and in 3.7 order is guaranteed.
-
Timothy Jannace over 5 years@roganjosh Whoa, thanks for sharing. I'll update my answer.
-
BARIS KURT about 2 yearsthis is not what exactly I want but it seems this is the only solution I have... Thank you.