list.index crashes when the element is not found

T

TkNeo

TkNeo said:
This is crazy

Crazy like a fox?

a = [1, 2, 3]
try:
a.index(99)
except:
a.append(99)
finally:
print a.index(99)

MY question: which exception should I actually be catching there?

ofcouse try catch is going to work but in ideality the index function
should return a -1 and no way in hell crash.
 
J

Jerry Hill

ofcouse try catch is going to work but in ideality the index function
should return a -1 and no way in hell crash.

It doesn't crash. It raises an exception. This is a pretty
fundamental concept of python programming. If you intend to program
in python, then you're going to have to deal with it.

Besides, returning -1 is a bad idea. Take a look at the following:

lst = [1, 2, 3]
pos = lst.index(99)
lst[pos] = 0
print lst

What do you expect the result to be? If lst.index(99) returned -1,
then lst would be [1, 2, 0], because negative indexes of a list are
perfectly valid in python -- they just count from the end of the list.
 
J

Jeff

The generally used idiom for that is:

lst = ['a', 'b', 'c']
if 'a' in lst:
foo = lst.index('a')
 
G

George Sakkis

Crazy like a fox?
a = [1, 2, 3]
try:
a.index(99)
except:
a.append(99)
finally:
print a.index(99)
MY question: which exception should I actually be catching there?

ofcouse try catch is going to work but in ideality the index function
should return a -1 and no way in hell crash.

Please refrain from making such inane comments after an hour or two of
toying with a new language. Read a good tutorial first (e.g.
http://diveintopython.org/toc/index.html) and come back if you have a
real question.

George
 
T

TkNeo

The generally used idiom for that is:

lst = ['a', 'b', 'c']
if 'a' in lst:
foo = lst.index('a')

Jeff - Gracias !!

I am fairly new to python. Thanks for the example code snippet above.
It is the same amount of code as receiving -1 and then checking for
doing an "if else" for -1 so now i don't feel bad. But, being new to
this paradigm, raising of an exception when it can't find the element
appears to be weird to me for some unexplainable reason.
 
T

TkNeo

On May 2, 1:58 pm, Nick J Chackowsky <[email protected]>
wrote:
TkNeo wrote:
WHAT ?
This is crazy
Crazy like a fox?
a = [1, 2, 3]
try:
a.index(99)
except:
a.append(99)
finally:
print a.index(99)
MY question: which exception should I actually be catching there?
ofcouse try catch is going to work but in ideality the index function
should return a -1 and no way in hell crash.

Please refrain from making such inane comments after an hour or two of
toying with a new language. Read a good tutorial first (e.g.http://diveintopython.org/toc/index.html) and come back if you have a
real question.

George

George - I am not trying to be negative in any way whatsoever.

0 - In my first post in this thread, i was just being sarcastic.
1- I have been using python for more than an year or so. Just not an
avid user as probably you are.
2. You suggest me to ask a "REAL" question. Real is a relative word.
What is real to you might not be real to lets say Guido van Rossum. I
met him a few months ago and he was very humble to all the stupid
questions i had.
3 The board does not specify the level of python developers who can
post questions.
4 -I have always heard that python community is based on sharing and
helping each other. Please keep those spirits up.

Thanks for replying and sorry for wasting your time.
TK
 
S

s0suk3

The generally used idiom for that is:
lst = ['a', 'b', 'c']
if 'a' in lst:
foo = lst.index('a')

Jeff - Gracias !!

I am fairly new to python. Thanks for the example code snippet above.
It is the same amount of code as receiving -1 and then checking for
doing an "if else" for -1 so now i don't feel bad. But, being new to
this paradigm, raising of an exception when it can't find the element
appears to be weird to me for some unexplainable reason.

I don't know how Python looks up internally 'a' in the list in the
statement "if 'a' in lst: ... ," but just from common sense, I think
it would have to do the work of finding it twice, first in the
statement "'a' in lst," and then in the statement "lst.index('a')." If
you had a lot of elements in the list, and/or you were looping a lot
of times to find a lot of things, it'd be better to do the try/except
form.
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top