list index()

Neil Cerutti horpner at yahoo.com
Thu Aug 30 14:14:21 EDT 2007


On 2007-08-30, zzbbaadd at aol.com <zzbbaadd at aol.com> wrote:
>> 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.

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.

-- 
Neil Cerutti



More information about the Python-list mailing list