472,127 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

implementation of "in" that returns the object.

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?
>>def inplusplus(value,listObj):
.... 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
Oct 22 '06 #1
6 901
In <ma**************************************@python.o rg>, Jorge Vargas
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?
But you already *have* a reference to that object!?
>>>def inplusplus(value,listObj):
... 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
Oct 22 '06 #2
Marc 'BlackJack' Rintsch wrote:
>>>>def inplusplus(value,listObj):
... 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
however,
>>a = [1.0, 2, 3]
>>my_in(1, a)
1
>>my_in(2.0, a)
2.0
>>my_in(3L, a)
3L

so it all depends on what the OP means by "is in".

</F>

Oct 22 '06 #3
"Jorge Vargas" <jo**********@gmail.comwrote in message
news:ma**************************************@pyth on.org...
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?
>>>def inplusplus(value,listObj):
... 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:
>>def inplusplus(value,listObj):
.... 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":
>>def inplusplus(value,listObj):
.... if value in listObj: return value
.... return False
....
>>inplusplus( 123456789,lst)
123456789
2. What happens if "False" is in the list? How would you know?

-- Paul
Oct 22 '06 #4
Jorge Vargas wrote:
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?
>def inplusplus(value,listObj):
... 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.

Oct 23 '06 #5
Jorge Vargas <jo**********@gmail.comwrote:
>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 -- si***@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
Oct 23 '06 #6
"Jorge Vargas" <jo**********@gmail.comwrites:
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.

Oct 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Pedro Werneck | last post: by
43 posts views Thread by markryde | last post: by
5 posts views Thread by Nicolas Pontoizeau | last post: by
350 posts views Thread by Lloyd Bonafide | last post: by
15 posts views Thread by =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.