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
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)
๐ŸŒ
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__) ...
Discussions

string - Python: Why Lists do not have a find method? - Stack Overflow
I was trying to write an answer to this question and was quite surprised to find out that there is no find method for lists, lists have only the index method (strings have find and index). ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why no .get(idx[, default]) on python list??
There was a lengthy thread on this on Python-Ideas a couple of years ago: https://mail.python.org/archives/list/python-ideas@python.org/thread/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/ The clearest explanation of the most common objection was from Marc-Andre Lemburg: dict.get() was added since the lookup is expensive and you want to avoid having to do this twice in the common case where the element does exist. It was not added as a way to hide away an exception, but instead to bypass having to generate this exception in the first place. dict.setdefault() has a similar motivation. list.get() merely safes you a line of code (or perhaps a few more depending on how you format things), hiding away an exception in case the requested index does not exist. If that's all you want, you're better off writing a helper which hides the exception for you. I argue that making it explicit that you're expecting two (or more) different list lengths in your code results in more intuitive and maintainable code, rather than catching IndexErrors (regardless of whether you hide them in a method, a helper, or handle them directly). So this is more than just style, it's about clarity of intent. More on reddit.com
๐ŸŒ r/Python
94
144
June 21, 2022
List of all Python dunder methods?

Sounds like a classic case of X-Y-problem. What are you trying to achieve?

I don't think trying to maintain a comprehensive list of dunder methods is a good idea. Consider that this list is bound to change with upcoming versions of Python and has already undergone considerable changes in the past. You may end up maintaining varying lists of magic methods for different versions of Python.

I think the goto approach here would be to just try to call the respective method from within a generic __getattribute__ and react appropriately to TypeError.

It may also be possible to analyse the proxied object's class dict (someobject.__class__.__dict__) to find out which dunder methods were defined on that class. Of course you may end up having to walk up the inheritance chain as well, so this is bound to get quite complicated.

More on reddit.com
๐ŸŒ r/Python
15
7
February 27, 2018
Can VSCode outline class members for Python file?
It's a bug in the Python extension. It works correctly if you opt out of using the new language server. More on reddit.com
๐ŸŒ r/vscode
5
2
July 1, 2018
๐ŸŒ
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'}
๐ŸŒ
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....
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)
January 18, 2026 - 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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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
January 23, 2026 - 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.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-search-elements-in-list-437838
How to search elements in list | LabEx
Learn efficient Python list search techniques to find, filter, and locate elements using built-in methods, index searching, and advanced filtering strategies for optimal data manipulation.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ find item in list in python
Find item in list in Python | Sentry
my_list = [4, 8] odd_number = next((n for n in my_list if n%2), None) print(odd_number) # will print None ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... David Y. โ€” January 30, 2023 ... Naveera A. โ€” July 12, 2022 ... David Y. โ€” November 15, 2023 ยท Test whether a string contains a substring in Python
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-find-the-string-in-a-list
Python program to find String in a List - GeeksforGeeks
July 23, 2025 - Let's explore the different ways to write a python program to find string in a list. ... The list.index() method returns the index of the first occurrence of the string in the list.
๐ŸŒ
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: