473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 945
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex | last post by:
I'm new to Oracle, so this question may sound silly. I have been given a list of Oracle tables (they may be views, I'm not sure) that are available to me. I can run simple SQL select statements...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
5
by: Nicolas Pontoizeau | last post by:
Hi, I am handling a mixed languages text file encoded in UTF-8. Theres is mainly French, English and Asian languages. I need to detect every asian characters in order to enclose it by a special...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
3
dlite922
by: dlite922 | last post by:
Hey guys, My brains asleep and I don't know what's wrong with my session class. I'm over riding session with sesstion_set_save_handler() in a class; When in my member functions (open,...
5
by: Muzammil | last post by:
i have problem with this operator "+" in doubly circular link list.(i think i have problem with return type). error is of instantiate error.(mean type dismatch) if any one can help me please...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.