Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the name of a list in a list of lists instead of its value?

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"?
like image 960
NewHere Avatar asked Nov 25 '25 23:11

NewHere


1 Answers

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.

like image 178
Timothy Jannace Avatar answered Nov 28 '25 15:11

Timothy Jannace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!