implementation of "in" that returns the object.
Sion Arrowsmith
siona at chiark.greenend.org.uk
Mon Oct 23 11:02:19 EDT 2006
Jorge Vargas <jorge.vargas at gmail.com> wrote:
>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
>...
try:
obj = listObj[listObj.index(value)]
# do something with obj
except ValueError:
# do whatever you're going to do if inplusplus returns False
Assuming you meant "if i == value" (as others have pointed out).
--
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
More information about the Python-list
mailing list