Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Why do the find and index methods work differently?

Tags:

In Python, find and index are very similar methods, used to look up values in a sequence type. find is used for strings, while index is for lists and tuples. They both return the lowest index (the index furthest to the left) that the supplied argument is found.

For example, both of the following would return 1:

"abc".find("b") [1,2,3].index(2) 

However, one thing I'm somewhat confused about is that, even though the two methods are very similar, and fill nearly the same role, just for different data types, they have very different reactions to attempting to find something not in the sequence.

"abc".find("d") 

Returns -1, to signify 'not found', while

[1,2,3].index(4) 

raises an exception.

Basically, why do they have different behaviors? Is there a particular reason, or is it just a weird inconsistency for no particular reason?

Now, I'm not asking how to deal with this – obviously, a try/except block, or a conditional in statement, would work. I'm simply asking what the rationale was for making the behavior in just that particular case different. To me, it would make more sense to have a particular behavior to say not found, for consistency's sake.

Also, I'm not asking for opinions on whether the reason is a good reason or not – I'm simply curious about what the reason is.

Edit: Some have pointed out that strings also have an index method, which works like the index method for lists, which I'll admit I didn't know, but that just makes me wonder why, if strings have both, lists only have index.

like image 662
Kevin Avatar asked Dec 05 '13 20:12

Kevin


People also ask

What is the difference between the index () function and the find () function?

Both index() and find() are identical in that they return the index position of the first occurrence of the substring from the main string. The main difference is that Python find() produces -1 as output if it is unable to find the substring, whereas index() throws a ValueError exception.

What is the difference between indexing and slicing in Python?

“Indexing” means referring to an element of an iterable by its position within the iterable. “Slicing” means getting a subset of elements from an iterable based on their indices.

How does index method work in Python?

The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.


1 Answers

This has always been annoying ;-) Contrary to one answer, there's nothing special about -1 with respect to strings; e.g.,

>>> "abc"[-1] 'c' >>> [2, 3, 42][-1] 42 

The problem with find() in practice is that -1 is in fact not special as an index. So code using find() is prone to surprises when the thing being searched for is not found - it was noted even before Python 1.0.0 was released that such code often went on to do a wrong thing.

No such surprises occur when index() is used instead - an exception can't be ignored silently. But setting up try/except for such a simple operation is not only annoying, it adds major overhead (extra time) for what "should be" a fast operation. Because of that, string.find() was added in Python 0.9.9 (before then, only string.index() could be used).

So we have both, and that persists even into Python 3. Pick your poison :-)

like image 198
Tim Peters Avatar answered Oct 01 '22 09:10

Tim Peters