list.index

Erik Max Francis max at alcyone.com
Fri Jun 6 16:04:00 EDT 2003


Fredrik Lundh wrote:

> the usual way to do this is:
> 
>     if self.pointmarker in self.selected_points:
>         colour = DEFAULT_SELECTED_POINT_COLOUR
> 
> you can also use "find", which does the same thing as "index" if
> successful, but returns -1 if the item is not found.
> 
>     if self.selected_points.find(self.pointmarker) >= 0:
>         colour = DEFAULT_SELECTED_POINT_COLOUR

Isn't that only for strings?

>>> help([].find)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'find'

In which case, the use of the `in' operator is more appropriate.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Forgiveness is another word for letting go.
\__/  Matthew Fox




More information about the Python-list mailing list