3

Can someone explain to me how the funcion list.index() functions? I have the following code:

def getPos(self,tile):
        print self.tiles[5][5]
        print tile
        try:
           myIndex = self.tiles.index(tile)
           #some code
        except:
           print "exception raised"
           #some code

The result:

<Tile.Tile instance at 0x36BCEB8>
<Tile.Tile instance at 0x36BCEB8>
exception raised

Do you have an idea why list.index() returns an exception although the tile variable is a reference to an element of tiles[][] ? Thanks a lot.

ps: btw I'm passing tiles[5][5] in this specific case

2
  • 1
    As an aside: Never use a bare except: - always be specific in what exceptions you want to catch, or you might catch some that you are not expecting. Commented Oct 30, 2011 at 10:32
  • Think about this for a minute. If some_list.index(some_item) returns i, then some_list[i] should return some_item. What are you expecting myIndex to be after your index call? If it's going to work, you'd need to be able to say self.tiles[myIndex]. But there's obviously no possible thing that myIndex could be to make that work. So self.tile.index(tile) can't be expected to work. Commented Oct 30, 2011 at 11:05

3 Answers 3

4

self.tiles appears to be a sequence (e.g. list or tuple) of sequences. Elements of self.tiles are sequences, not tiles.

self.tiles.index(tile) tries to find a sequence which equals tile, and fails.

Try instead:

def getPos(self,tile):
    for i,row in enumerate(self.tiles):
        for j,elt in enumerate(row):
            if tile == elt:
                return (i,j)
    raise ValueError('no tile found')
Sign up to request clarification or add additional context in comments.

2 Comments

It works too but it's a little less efficient than Tim Pietzcker's solution. Thanks though.
If you use .index then every time the tile is not in the row, a ValueError will the raised. To cycle through all the rows, you'll have to use try...except. When you expect lots of exceptions to be raised, using if tends to be faster than try..except.
3

While the element does exist, it is not directly a member of tiles:

  • tiles is a two-dimensional list (a list of lists).
  • tiles[5] is a list of Tiles.
  • tiles[5][5] is a single Tile.

Python does not recursively descend into a multidimensional list to find the element you're looking for. Therefore tiles.index(tile) fails; tiles[5].index(tile) would work.

To illustrate:

>>> l = [[1,2], [3,4]]
>>> l.index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 4 is not in list
>>> l[1].index(4)
1

Comments

-1

How come tiles[5][5] and tile points to the same instance? It appears to me that you grab entire this object at tile[5][5] as tile and try to locate it as an element of it. dont understand your intention

1 Comment

sorry that i didt have answer, but i thought my observation leads to resolve cause of trouble. maybe it was some my misunderstanding of issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.