This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add a 'find' method (a'la str) to list
Type: enhancement Stage: resolved
Components: Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: nemeskeyd, rhettinger, steve.dower
Priority: normal Keywords:

Created on 2021-09-23 15:05 by nemeskeyd, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg402497 - (view) Author: Dávid Nemeskey (nemeskeyd) Date: 2021-09-23 15:05
There is an unjustified asymmetry between `str` and `list`, as far as lookup goes. Both have an `index()` method that returns the first index of a value, or raises a `ValueError` if it doesn't exist. However, only `str` has the `find` method, which returns -1 if the value is not in the string.

I think it would make sense to add `find` to `list` as well. For starters, it would make the API between the two sequence types more consistent. More importantly (though it depends on the use-case), `find` is usually more convenient than `index`, as one doesn't have to worry about handling an exception. As a bonus, since `list` is mutable, it allows one to write code such as

    if (idx := lst.find(value)) == -1:
        lst.append(value)
    call_some_function(lst[idx])

, making the method even more useful as it is in `str`.
msg402502 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-09-23 15:55
Strings are already special in that str.index() and str.find() both find substrings, while list.index() only finds a single element.

If .find() also searched for a substring of the list, I think this could be helpful. Even more so if it used an efficient algorithm (bearing in mind that the arbitrary comparisons between elements - something else that doesn't exist in strings - would make this complicated).

This is probably something to bring up on the python-ideas mailing list first, anyway. Symmetry is not a sufficient reason in itself to add new APIs - often the asymmetry exists because the "missing" one is only there for legacy/compatibility reasons, not because it is a good API.
msg402513 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-23 17:25
This is one bit of symmetry that we shouldn't pursue.  The find() methods have been a recurring source of bugs because of failing to check for a -1 result but having that be a valid index into the sequence.

Also, the sequence APIs are long established.  If find() were really needed, we would have felt the pain and added it long ago.

As Steve says, you can take this to python-ideas, but my recommendation is to not do it all.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89434
2021-09-23 17:25:31rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg402513

resolution: rejected
stage: resolved
2021-09-23 15:55:31steve.dowersetnosy: + steve.dower
messages: + msg402502
2021-09-23 15:05:23nemeskeydsettype: enhancement
2021-09-23 15:05:06nemeskeydcreate