implementation of "in" that returns the object.

J

Jorge Vargas

Hi

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?
.... for i in listObj:
.... if i is value:
.... return value
.... return False
....
l = [1,2,3,4]
print inplusplus(2,l) 2
print inplusplus(9,l) False
print inplusplus(1,l) 1
l.append(0)
print inplusplus(0,l)
0
 
M

Marc 'BlackJack' Rintsch

Jorge Vargas said:
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?

But you already *have* a reference to that object!?
... for i in listObj:
... if i is value:
... return value
... return False
...

def my_in(value, sequence):
if value in sequence:
return value
else:
return False

Ciao,
Marc 'BlackJack' Rintsch
 
P

Paul McGuire

Jorge Vargas said:
Hi

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?
... for i in listObj:
... if i is value:
... return value
... return False
...
l = [1,2,3,4]
print inplusplus(2,l) 2
print inplusplus(9,l) False
print inplusplus(1,l) 1
l.append(0)
print inplusplus(0,l)
0

Just a couple of quick comments:
1. "if i is value" will check for identity, not equality. Your example with
small integers relies on a nonportable CPython implementation of using
cached objects. Check out this behavior:
.... for i in listObj:
.... if i is value:
.... return value
.... return False
....
a = 5
lst = [ 1,3,5,7 ]
inplusplus(5,lst) 5
inplusplus(a,lst) 5
lst.append( 123456789 )
inplusplus( 123456789,lst)
False

Instead of this loopy "is" test, just use "in":
.... if value in listObj: return value
.... return False
....123456789


2. What happens if "False" is in the list? How would you know?

-- Paul
 
P

Paddy

Jorge said:
Hi

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?
... for i in listObj:
... if i is value:
... return value
... return False
...
l = [1,2,3,4]
print inplusplus(2,l) 2
print inplusplus(9,l) False
print inplusplus(1,l) 1
l.append(0)
print inplusplus(0,l)
0

You mentioned a ist of objects.
The following example will return the actual object matched in the ist
allowing you to later change any mutable object returned and see the
change reflected in the list:
x = [[0], [1], [2]]
y = [1]
def inref(val, lst):
.... try:
.... return lst[ lst.index(val) ]
.... except ValueError:
.... return False
....
z = inref(y, x)
z [1]
z[0] = 33
z [33]
x
[[0], [33], [2]]


Hope this helps - Paddy.
 
S

Sion Arrowsmith

Jorge Vargas said:
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?

... 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).
 
P

Paul Rubin

Jorge Vargas said:
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?

... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top