473,574 Members | 3,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list index()

What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3. If nothing else, add
a function that doesn't throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.

Aug 30 '07 #1
96 4528
On Wed, 29 Aug 2007 23:44:33 -0700, zzbbaadd wrote:
What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3. If nothing else, add
a function that doesn't throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.
Write such a function yourself, it is quite easy after all. I very seldom
use the `list.index()` method. What do your millions situations look like?
Maybe there is a better data structure than lists for those situations!?

Ciao,
Marc 'BlackJack' Rintsch
Aug 30 '07 #2
zz******@aol.co m writes:
What's with the index() function of lists throwing an exception on not
found?
It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.
Let's hope this is rectified in Python 3. If nothing else, add a
function that doesn't throw an exception.
You can easily create one:

def get_an_index_ev en_if_not_found (the_list, the_item):
bogus_index_val ue = object()
try:
index = the_list.index( the_value)
except ValueError:
index = bogus_index_val ue
return index

It's up to you to figure out what bogus_index_val ue you want to
use. The rest of us will continue to catch the exception where needed.

--
\ "Reichel's Law: A body on vacation tends to remain on vacation |
`\ unless acted upon by an outside force." -- Carol Reichel |
_o__) |
Ben Finney
Aug 30 '07 #3
On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
zz******@aol.co m writes:
>What's with the index() function of lists throwing an exception on not
found?

It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.
What about -1? C programmers do this all the time. :-)

Ciao,
Marc 'BlackJack' Rintsch
Aug 30 '07 #4
zz******@aol.co m a écrit :
What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3. If nothing else, add
a function that doesn't throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.
What's with using your brain instead of whining ?

Aug 30 '07 #5
Marc 'BlackJack' Rintsch <bj****@gmx.net writes:
On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.

What about -1? C programmers do this all the time. :-)
I don't believe you've contradicted me :-)

--
\ "If you ever drop your keys into a river of molten lava, let |
`\ 'em go, because, man, they're gone." -- Jack Handey |
_o__) |
Ben Finney
Aug 30 '07 #6
Bruno Desthuilliers <br************ ********@wtf.we bsiteburo.oops. comwrites:
What's with using your brain instead of whining ?
Now now, no need for snappiness. If you don't feel a constructive
response is merited, please ignore.

--
\ "A lot of people are afraid of heights. Not me, I'm afraid of |
`\ widths." -- Steven Wright |
_o__) |
Ben Finney
Aug 30 '07 #7
Ben Finney <bi************ ****@benfinney. id.auwrites:
def get_an_index_ev en_if_not_found (the_list, the_item):
Bah. Should be "…(the_li st, the_value):".

--
\ "Don't worry about people stealing your ideas. If your ideas |
`\ are any good, you'll have to ram them down people's throats." |
_o__) -- Howard Aiken |
Ben Finney
Aug 30 '07 #8
Ben Finney a écrit :
Bruno Desthuilliers <br************ ********@wtf.we bsiteburo.oops. comwrites:
>What's with using your brain instead of whining ?

Now now, no need for snappiness. If you don't feel a constructive
response is merited, please ignore.
Yes, you're right. Sorry.

Aug 30 '07 #9
On Wed, 2007-08-29 at 23:44 -0700, zz******@aol.co m wrote:
What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3.
You're assuming that this behavior is a mistake. It's not, and
consequently, it won't be "rectified" .
If nothing else, add
a function that doesn't throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.
How could it not be an exception, in the plain English sense of the
word? Most certainly you're asking for the index because you want to do
something with the index. If the item is not found, you have no index,
so that's a special case that must be handled separately. There is no
logical difference between handling that special case in an except
clause versus handling it with an if-branch.

Is the Pythonic way

try:
i = somelist.index( thing)
# Do something with i
except IndexError:
# Do something if thing not found

really that much worse than the theoretical alternative

i = somelist.index_ that_returns_an _indicator(thin g)
if i!=ThingNotFoun dIndicator:
# Do something with i
else:
# Do something if thing not found

?

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1479
by: questions? | last post by:
I want to do list index function. >>> y= >>> y >>> y.index Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsubscriptable object It works with y=. Anyone has any hint, what's the reason
9
17397
by: William Meyer | last post by:
hi, I need to get the index of an object in a list. I know that no two objects in the list are the same, but objects might evaluate as equal. for example list = for object in list: objectIndex = list.index(object) print objectIndex
8
28468
by: sam | last post by:
hey everybody, this is my first time posting here. i'm pretty new to python and programming in general (as you'll soon work out for yourselves...) i'm trying to code a version of a selection sort and the heart of the code is as follows (no_lines is simply the number of items to be sorted, read out of an input file): for j in range(0,...
35
29204
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. import shutil, os, sys
7
10771
by: python101 | last post by:
My code s= for k in range(len(s)): st=s.count(s) if st>=2: print s.pop(s.index(s)) Traceback (most recent call last):
3
2411
by: Riccardo Murri | last post by:
Hello, I have some code that stops when trying to find a graph in a list of similar graphs:: (Pydb) list 110 try: 111 canonical = self.base 112 except ValueError: 113 raise ValueError, \
2
3714
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The strange thing is that it will sometimes run for 10 iterations sometimesfor only a few and sometimes won't run at all (seemingly arbitrary). Here is...
5
18638
by: Mr.SpOOn | last post by:
Hi, is there any way to search elements in a list using wildcards? I have a list of various elements and I need to search for elements starting with 'no', extract them and put in a new list. I was thinking about something like: mylist.index('no*') Of course this doesn't work.
2
4377
by: Chriskim | last post by:
I wrote a program that uses PyQt4, mathplotlib to generates graphs. I made a executable file using Py2exe. Everything is working fine, but when I exit from my program, a window message pops out and tells me to look at the log file. In the log file, here is what they said ...
0
7843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8140
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3776
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.