implementation of "in" that returns the object.

Paul Rubin http
Mon Oct 23 17:15:32 CEST 2006

"Jorge Vargas" <jorge.vargas at> writes:
> I need to check if an object is in a list AND keep a reference to the
> object I have done it this way but is there a better one?
> >>> def inplusplus(value,listObj):
> ...     for i in listObj:
> ...             if i is value:
> ...                     return value
> ...     return False

That's bug-prone.  Suppose the object's value is false (i.e.  it's the
empty string, or None, or the boolean False, or whatever)?  You're
best off raising an exception if the value is not found.  You could
also do something like that using the list.index method.

More information about the Python-list mailing list