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.

Title: Adding the method find() to list
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: marcospb19, remi.lapeyre, rhettinger
Priority: normal Keywords:

Created on 2020-05-06 13:49 by marcospb19, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg368254 - (view) Author: João Marcos (marcospb19) Date: 2020-05-06 13:49

When trying to search the position of an element inside a list, we should use the `in` operator to first check if the element exists, and then use the `index` method to obtain the index.

`in` (__contains__) runs a linear search to return the boolean.
`index`        also runs a linear search to return the index.

This makes the code slower, because we need to search for the same item twice.


Similar to str.find(), list.find() should be implemented, where -1 is returned when the element isn't present

# Since there's no list.find(), this is my workaround to achieve making only one linear search per query
def find(container: list, index: int) -> int:
    """ Str.find() behavior but for lists """
        return container.index(index)
    except ValueError:
        return -1

# Example driver code:
index = find(list, possible_element)
if index_of_element == -1:
    pass # Not found
    pass # Found
msg368255 - (view) Author: João Marcos (marcospb19) Date: 2020-05-06 14:04
This is my first issue, is this the right place to discuss enhancements?
msg368267 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-05-06 15:28
Hi João, ideas like this can also be proposed first on the python-ideas mailing list but as you said in your post there is already a method to do this and it raises ValueError when it is not found which is a common idiom in Python. Other objects don't often have two versions of the same method, one that raises an exception on error and one that returns a sentinel, most only have the one that raises.

Notice that your example is not simpler with your proposal:

# Example driver code:
index = find(list, possible_element)
if index_of_element == -1:
    pass # Not found
    pass # Found

is a hard to read than and actually longer:

    index = list.index(possible_element)
except ValueError:
    index = -1

if you really care about the number of lines you could use, while I don't recommend it:

try: index = list.index(possible_element)
except ValueErrort: index = -1

Also adding a new method to list would mean adding it to all sequence objects and that is asking for a lot.
msg368268 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-06 15:30
Please take this to Python ideas.  My understanding is that index() is supposed to be the one-way-to-do-it going forward.  The find() API proved to be problematic because -1 is a valid index and people were hitting bugs by failing to check for the -1 and then doing an unintended s[-1].
msg368271 - (view) Author: João Marcos (marcospb19) Date: 2020-05-06 15:49
Thanks for the replies!.

Here's the link for discussion in the Python Ideas:
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84711
2020-05-06 15:49:07marcospb19setmessages: + msg368271
2020-05-06 15:30:09rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg368268

resolution: rejected
stage: resolved
2020-05-06 15:28:43remi.lapeyresetnosy: + remi.lapeyre
messages: + msg368267
2020-05-06 14:04:38marcospb19setmessages: + msg368255
components: + Library (Lib)
2020-05-06 13:49:00marcospb19create