list index()

Z

zzbbaadd

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

Marc 'BlackJack' Rintsch

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
 
B

Ben Finney

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_even_if_not_found(the_list, the_item):
bogus_index_value = object()
try:
index = the_list.index(the_value)
except ValueError:
index = bogus_index_value
return index

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

Marc 'BlackJack' Rintsch

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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 ?
 
B

Ben Finney

Bruno Desthuilliers said:
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.
 
B

Bruno Desthuilliers

Ben Finney a écrit :
Now now, no need for snappiness. If you don't feel a constructive
response is merited, please ignore.

Yes, you're right. Sorry.
 
C

Carsten Haese

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(thing)
if i!=ThingNotFoundIndicator:
# Do something with i
else:
# Do something if thing not found

?
 
M

Marshall T. Vandegrift

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.

The Python string types have both the method `index()` which throws an
exception and the method `find()` which implements the same behavior but
returns -1 for not-found substrings. I would naively have assumed the
`list` type to provide both as well, but it provides only `index()`.

Anyone know the reason for this lack of parallelism?

-Marshall
 
M

Marc 'BlackJack' Rintsch

The Python string types have both the method `index()` which throws an
exception and the method `find()` which implements the same behavior but
returns -1 for not-found substrings. I would naively have assumed the
`list` type to provide both as well, but it provides only `index()`.

Anyone know the reason for this lack of parallelism?

Historical reasons and IIRC the `find()` method on strings will go away in
Python 3.0.

Ciao,
Marc 'BlackJack' Rintsch
 
Z

zzbbaadd

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.

for str:

find( sub[, start[, end]])
Return the lowest index in the string where substring sub is
found, such that sub is contained in the range [start, end]. Optional
arguments start and end are interpreted as in slice notation. Return
-1 if sub is not found.

-1 is used in other languages as well.
 
Z

zzbbaadd

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.

In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.
I have used exceptions in other languages and only do so on logic that
should never happen. In this case it is known that some of the files
will not be in both lists. I just want to know which ones.
 
R

Robert Kern

In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.
I have used exceptions in other languages and only do so on logic that
should never happen.

Python is different than those languages. Exceptions are used much more
frequently in Python and often for things that will *definitely* happen not just
those things that shouldn't.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Steve Holden

In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.
I have used exceptions in other languages and only do so on logic that
should never happen. In this case it is known that some of the files
will not be in both lists. I just want to know which ones.
And, as is so often the case, once the *real* problem is stated a more
elegant solution become available - in this case, using sets.

afiles = set(os.listdir(dira))
bfiles = set(os.listdir(dirb))

for f in (afiles - bfiles):
print "Not common:", f


You can also generate the files that are in one directory but ot the
other with

(afiles | bfiles) - (afiles & bfiles)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
C

Carsten Haese

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.

In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.
[...]

list.index() is the wrong tool for that job. Python has sets, use them.
 
C

Carsten Haese

I knew there would be at least one religious zealot.

While I agree that Bruno's response was perhaps needlessly snippy, your
original question was needlessly inflammatory, as if you somehow wanted
some "religious zealot" to act the way Bruno did. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
 
N

Neil Cerutti

In my case of have done os.listdir() on two directories. I want
to see what files are in directory A that are not in directory
B.

In that case list.find would not be much of a win, but sets might
be.

not_in_both = list(set(os.listdir("A")) - set(os.listdir("B")))
I have used exceptions in other languages and only do so on
logic that should never happen. In this case it is known that
some of the files will not be in both lists. I just want to
know which ones.

"Exceptions" has become somewhat a misnomer. iterators are
implemented using exceptions, and there's hardly anything more
common in modern Python code. The hair shirts and thumb-screws
necessary for using exceptions correctly in C++, aren't needed in
Python.
 

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,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top