16

I've been tinkering in python this week and I got stuck on something.
If I had a 2D list like this:

myList = [[1,2],[3,4],[5,6]]

and I did this

>>>myList.index([3,4])

it would return

1

However, I want the index of something in side one of the lists, like this

    >>>myList.index(3)

and it would return

1, 0

Is there anything that can do this?

Cheers

tituszban
  • 4,797
  • 2
  • 19
  • 30
Sam Jarman
  • 7,277
  • 15
  • 55
  • 100

8 Answers8

25

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)
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
5

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)
kevpie
  • 25,206
  • 2
  • 24
  • 28
2

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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

There is nothing that does this already, unless it's in numpy, which I don't know much about. This means you'll have to write code that does it. And that means questions like "What does [[1, 2], [2, 3], [3, 4]].index(3) return?" are important.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
0

Try this! this worked for me :)

def ret_pos(mylist,val_to_find):
    for i in (len(mylist)):
        for j in (len(i)):
            if mylist[i][j]== val_to_find:
                postn=[i,j]
    return(postn);
  • Hello and welcome to StackOverflow. Please add some explanation to your answer so it becomes more valuable for other users. See http://stackoverflow.com/help/how-to-answer – wmk Jan 28 '17 at 21:11
0

Based on kevpie's answer I managed to get a 2D list containing the coordinates of all occurences

myList = [[0,1],[1,1],[0,0],[1,0]]
coordsList = [[x, y] for x, li in enumerate(myList) for y, val in enumerate(li) if val==1]

Now coordsList contains all indexes for value 1 in myList :

[[0, 1], [1, 0], [1, 1], [3, 0]]
0
def td(l,tgt):
    rtr=[]
    for sub in l:
        if tgt in sub:
            rtr.append(    (l.index(sub),sub.index(tgt))    )

    return rtr        


myList = [[1,2],[3,4],[5,6]]

print td(myList,3)

This will return more than one instance of the sub list, if any.

dawg
  • 98,345
  • 23
  • 131
  • 206
0

Caution: The first answer up there:

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

will cause a lot of errors as long as there are any equal/identical elements, for any v in each x. Because x.index(v) only returns the first (leftmost) index of any element that has the same value of v, in x, NOT the actual index of the current v. That is, this only works if all elements in each row are unique.

Instead, try this to avoid non-unique values:

import numpy
arr_2D = numpy.array(...)
                  
for x, row in enumerate(arr_2D.tolist()):
    for y, cell in enumerate(row):
    cell_pos = (x, y)
ZWW
  • 1