Determinig position of a element in a list

Erik Max Francis max at alcyone.com
Mon Aug 25 19:01:47 EDT 2003


Przemo Drochomirecki wrote:

> i'm wondering if there is any tricky way for doing following thing:
>     A - list of distinct integers  (e.x. A = [1,3,7,11,14,15])
>     very fast function determinig position of number x in list A
>     or -1 if x doesnt belong to A
> Operator IN returns only false/true values
> i can implement function index (e.x. index(5,A) = -1, index(7,A) = 2),
> but
> maybe there's is simpler(builtin?) solution

Yep, the -- wait for it -- .index method on the list object:

>>> A = [1, 3, 7, 11, 14, 15]
>>> A.index(7)
2

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Every exit is an entry somewhere else.
\__/  Tom Stoppard




More information about the Python-list mailing list