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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With