Biosci3c
Biosci3c

Reputation: 772

Python: Find item in multidimensional list

I have a list of lists, a snippet of which is below:

x_attrib = []

self.x_attrib.append(["Is_virtual", False, 'virtual', 'flag'])
self.x_attrib.append(["X_pos", None, 'pos/x', 'attrib'])
self.x_attrib.append(["Y_pos", None, 'pos/y', 'attrib'])

I want make a functional that returns the index of the item at the first position (i.e. I want to pass "X_pos" to a function, and have it return 1).

How can I do this?

Upvotes: 3

Views: 7350

Answers (1)

Winston Ewert
Winston Ewert

Reputation: 45079

If I understand correctly, you need to something like this:

def find_it(key):
   for index, sublist in enumerate(lists):
       if sublist[0] == key:
            return index

Having said that your code looks like you are solving the more general problem incorrectly. i.e. that list look like a bad idea. Without a better idea of what you are doing I cannot be certain.

Upvotes: 4

Related Questions