list index()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Sep 1 15:57:31 EDT 2007


On Sat, 01 Sep 2007 13:37:29 -0600, Michael L Torrie wrote:

> What's wrong, then, with doing:
> 
> if i in list:
>    print list.index(i)

If `i` is in the list this does the linear lookup twice.

> If we were to program this .index() method in some language that
> enforces contracts, like haskell, then we'd say that .index() expects a
> value that exists in the list.  So if you violate the contract, why
> should you expect to *not* get an exception.  Doing it any other way,
> though, makes the code a lot more error prone.

Does Haskell exceptions?  In Haskell I would expect such a function to
return the `Maybe` type.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list