[Tutor] list.index method

amoreira@mercury.ubi.pt amoreira@mercury.ubi.pt
Fri, 12 May 2000 10:40:35 +0000

```Hello!
I've been thinking about the definition of some list methods, like
list.index and list.remove, and there's something that is itching me,
ever since I started using python (not that very long ago, anyway). Take
list.index(x), for instance. This method returns the position of the
first list element that is equal to x. So, when some of the list items
are equal, there may be some problems. Take an example:
>>> l=[]
>>> for i in range(3):
...  l.append(abs(i-1.))
...
#This is a funny way to define this simple list, but I want it to verify
l[0]==l[2] *and* not(l[0] is l[2]),
#that is, l[0] and l[2] are not references to the same object, but the
objects wich they point to are #equal. (Sorry if I can't explain this
any better, I'm a Fortran programmer not very familiar with this
#object, pointer, and god knows what else mess)
#Let's check this list:
>>> l
[1.0, 0.0, 1.0]
>>> l[0] == l[2]
1
>>> l[0] is l[2]
0
#OK, this is exactly what I wanted. Now look at this:
>>> for x in l:
...  print l.index(x)
...
0
1
0

What is itching me is that I'd rather have this code print 0,1,2, the
indexes of each of the elements of the list. I'd say that it would be
better if the definition of list.index(x) would be: return the smallest
i such that list[i] is x, instead of the smallest i such that
list[i]==x...
Is there any reason for this behaviour?
Sorry for such a long post, and thank you all!
Ze
amoreira@mercury.ubi.pt

```