Python: Return 2 ints for index in 2D lists given item

If you are doing many lookups you could create a mapping.

>>> myList = [[1,2],[3,4],[5,6]]
>>> d = dict( (j,(x, y)) for x, i in enumerate(myList) for y, j in enumerate(i) )
>>> d
{1: (0, 0), 2: (0, 1), 3: (1, 0), 4: (1, 1), 5: (2, 0), 6: (2, 1)}
>>> d[3]
(1, 0)

Try this:

def index_2d(myList, v):
    for i, x in enumerate(myList):
        if v in x:
            return (i, x.index(v))

Usage:

>>> index_2d(myList, 3)
(1, 0)

Using simple genexpr:

def index2d(list2d, value):
    return next((i, j) for i, lst in enumerate(list2d) 
                for j, x in enumerate(lst) if x == value)

Example

print index2d([[1,2],[3,4],[5,6]], 3)
# -> (1, 0)