Search in 2D list using python to find x,y position

arrays, list, python, python-2.7, search

Solution

You don't need to define `no_classes` yourself. Use `enumerate()`:

def in_list(c, classes):
    for i, sublist in enumerate(classes):
        if c in sublist:
            return i
    return -1

Problem

I have 2D list and I need to search for the index of an element. As I am begineer to programming I used the following function: ``` def in_list(c): for i in xrange(0,no_classes): if c in classes[i]: return i; return -1 ``` Here classes is a 2D list and no_classes denotes the number of classes i.e the 1st dimesntion of the list. -1 is returned when c is not in the araray. Is there any I can optimize the search?

Original source