As for your first question: "if item is in my_list:" is perfectly fine and should work if item equals one of the elements inside my_list. The item must exactly match an item in the list. For instance, "abc" and "ABC" do not match. Floating point values in particular may suffer from inaccuracy. For instance, 1 - 1/3 != 2/3.

As for your second question: There's actually several possible ways if "finding" things in lists.

Checking if something is inside

This is the use case you describe: Checking whether something is inside a list or not. As you know, you can use the in operator for that:

3 in [1, 2, 3] # => True

Filtering a collection

That is, finding all elements in a sequence that meet a certain condition. You can use list comprehension or generator expressions for that:

matches = [x for x in lst if fulfills_some_condition(x)]
matches = (x for x in lst if x > 6)

The latter will return a generator which you can imagine as a sort of lazy list that will only be built as soon as you iterate through it. By the way, the first one is exactly equivalent to

matches = filter(fulfills_some_condition, lst)

in Python 2. Here you can see higher-order functions at work. In Python 3, filter doesn't return a list, but a generator-like object.

Finding the first occurrence

If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the else clause as well, which is not really well-known). You can also use

next(x for x in lst if ...)

which will return the first match or raise a StopIteration if none is found. Alternatively, you can use

next((x for x in lst if ...), [default value])

Finding the location of an item

For lists, there's also the index method that can sometimes be useful if you want to know where a certain element is in the list:

[1,2,3].index(2) # => 1
[1,2,3].index(4) # => ValueError

However, note that if you have duplicates, .index always returns the lowest index:......

[1,2,3,2].index(2) # => 1

If there are duplicates and you want all the indexes then you can use enumerate() instead:

[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
Answer from Niklas B. on Stack Overflow
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Adding the method find() to list - Ideas - Discussions on Python.org
May 6, 2020 - Now the real topic: . . . โ€œโ€" PROBLEM: 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__) ...
Top answer
1 of 14
1823

As for your first question: "if item is in my_list:" is perfectly fine and should work if item equals one of the elements inside my_list. The item must exactly match an item in the list. For instance, "abc" and "ABC" do not match. Floating point values in particular may suffer from inaccuracy. For instance, 1 - 1/3 != 2/3.

As for your second question: There's actually several possible ways if "finding" things in lists.

Checking if something is inside

This is the use case you describe: Checking whether something is inside a list or not. As you know, you can use the in operator for that:

3 in [1, 2, 3] # => True

Filtering a collection

That is, finding all elements in a sequence that meet a certain condition. You can use list comprehension or generator expressions for that:

matches = [x for x in lst if fulfills_some_condition(x)]
matches = (x for x in lst if x > 6)

The latter will return a generator which you can imagine as a sort of lazy list that will only be built as soon as you iterate through it. By the way, the first one is exactly equivalent to

matches = filter(fulfills_some_condition, lst)

in Python 2. Here you can see higher-order functions at work. In Python 3, filter doesn't return a list, but a generator-like object.

Finding the first occurrence

If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the else clause as well, which is not really well-known). You can also use

next(x for x in lst if ...)

which will return the first match or raise a StopIteration if none is found. Alternatively, you can use

next((x for x in lst if ...), [default value])

Finding the location of an item

For lists, there's also the index method that can sometimes be useful if you want to know where a certain element is in the list:

[1,2,3].index(2) # => 1
[1,2,3].index(4) # => ValueError

However, note that if you have duplicates, .index always returns the lowest index:......

[1,2,3,2].index(2) # => 1

If there are duplicates and you want all the indexes then you can use enumerate() instead:

[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
2 of 14
279

If you want to find one element or None use default in next, it won't raise StopIteration if the item was not found in the list:

first_or_default = next((x for x in lst if ...), None)
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ why doesn't python lists have a .find() method?
r/Python on Reddit: Why doesn't python lists have a .find() method?
May 6, 2022 -

Doesn't it make sense for lists to have a find method?

class MyList(list):
    def find(self, func):
        for i in self:
            if func(i):
                return i
        return None
        # or raise ValueError?


users = [
    {
        'id': 1,
        'name': 'john',
    },
    {
        'id': 2,
        'name': 'anna',
    },
    {
        'id': 3,
        'name': 'bruce',
    }
]

my_list = MyList(users)
user_2 = my_list.find(lambda i: i['id'] == 2)
print(user_2)  # {'id': 2, 'name': 'anna'}
Top answer
1 of 3
7

I don't know why or maybe is buried in some PEP somewhere, but i do know 2 very basic "find" method for lists, and they are array.index() and the in operator. You can always make use of these 2 to find your items. (Also, re module, etc)

2 of 3
4

I think the rationale for not having separate 'find' and 'index' methods is they're not different enough. Both would return the same thing in the case the sought item exists in the list (this is true of the two string methods); they differ in case the sought item is not in the list/string; however you can trivially build either one of find/index from the other. If you're coming from other languages, it may seem bad manners to raise and catch exceptions for a non-error condition that you could easily test for, but in Python, it's often considered more pythonic to shoot first and ask questions later, er, to use exception handling instead of tests like this (example: Better to 'try' something and catch the exception or test if its possible first to avoid an exception?).

I don't think it's a good idea to build 'find' out of 'index' and 'in', like

if foo in my_list:
   foo_index = my_list.index(foo)
else:
    foo_index = -1 # or do whatever else you want

because both in and index will require an O(n) pass over the list.

Better to build 'find' out of 'index' and try/catch, like:

try:
    foo_index = my_list.index(foo)
catch ValueError:
    foo_index = -1 # or do whatever else you want

Now, as to why list was built this way (with only index), and string was built the other way (with separate index and find)... I can't say.

๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf blog โ€บ pdf tools โ€บ python-find-in-lists
Python Find in Lists (How It Works For Developers)
April 30, 2024 - Create a Python file to find an element in a list. Find Element Exists Using the "in" Operator. Find Element Exists Using the list "index()" Method.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
Example 2: In this example, we try to find the index of 'yellow' in the list and handle the error with a try-except block if it's not found. ... a = ['red', 'green', 'blue'] try: index = a.index('yellow') print(a) except ValueError: print("Not Present") ... Example 3: In this example, we are finding the index of the tuple ("Bob", 22) in a list of tuples and index() will return the position of its first occurrence.
Published ย  April 27, 2025
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.3 documentation
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (โ€œlast-in, first-outโ€). To add an item to the top of the stack, use append(). To retrieve an item from the top of the ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_find.asp
Python String find() Method
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... The find() method finds the first occurrence of the specified value....
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_list.asp
Python List/Array Methods
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Python has a set of built-in methods that you can use on lists/arrays.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List โ€“ How to Find the Index of an Item or Element in a List
February 24, 2022 - There are a few ways to achieve this, and in this article you will learn three of the different techniques used to find the index of a list element in Python. ... Use the index() method to find the index of an item 1.Use optional parameters ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-find-string-in-list
Python Find String in List: Complete Guide with Examples | DigitalOcean
February 25, 2025 - Learn how to find strings in a Python list using methods like in operator, count(), and custom logic. Explore examples, handle errors, and debug issues.
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ python-find-in-list-a-comprehensive-guide-6c6
Python Find in List: A Comprehensive Guide - DEV Community
January 8, 2025 - It returns True if the element is present; otherwise, it returns False. ... This approach is both intuitive and efficient for basic membership checks. ... If you need the position of an element in the list, the list.index() method is the go-to ...
๐ŸŒ
Keploy
keploy.io โ€บ home โ€บ community โ€บ finding elements in a list using python
Finding Elements in a List using Python | Keploy Blog
November 18, 2024 - Master element lookup in Python lists using in, index(), loops, and more. Learn with clear examples and optimize your search operations.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-find-in-list
Python Find in List - Javatpoint
Python Find in List with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ list-methods-python
Python List methods - GeeksforGeeks
... In the code below, we will reverse the order of the elements in the list. ... Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements.
Published ย  October 24, 2024
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python lists
Python Lists | Python Education | Google for Developers
The range() function is often used with a for-loop to create a traditional numeric loop. Various list methods like append(), insert(), extend(), index(), remove(), sort(), reverse(), and pop() are available to modify and interact with lists.
๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf for python โ€บ ironpdf for python blog โ€บ python pdf tools โ€บ python find in list
Python Find in List (How It Works For Developers)
June 23, 2025 - Let's incorporate a Python list operation with IronPDF extracted text. The following code demonstrates how to use the in operator to find specific text within the extracted content and then count the number of occurrences of each keyword. We can also use the list comprehension method to find complete sentences which contain the keywords:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list
Python List Methods | Programiz
Python has a lot of list methods that allow us to work with lists. In this reference page, you will find all the list methods to work with Python List.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ find item in list in python
Find item in list in Python | Sentry
The Problem How can I find an item in a Python list? The Solution To check whether a list contains an item, we can use the Python in operator: To return theโ€ฆ