You can use a list comprehension with enumerate:

indices = [i for i, x in enumerate(my_list) if x == "whatever"]

The iterator enumerate(my_list) yields pairs (index, item) for each item in the list. Using i, x as loop variable target unpacks these pairs into the index i and the list item x. We filter down to all x that match our criterion, and select the indices i of these elements.

Answer from Sven Marnach on Stack Overflow
🌐
IronPDF
ironpdf.com › ironpdf blog › pdf tools › python-find-in-lists
Python Finding Elements in Lists (Beginner-Friendly Guide)
April 24, 2026 - Find Duplicates Using List Comprehension. Find Element Exists Using the "filter()" Function. Find Element Exists Using External Libraries. Install Python: Make sure Python is installed on the local machine, or visit python.org to follow the steps to install Python.
Discussions

python - Find a value in a list - Stack Overflow
EDIT FOR REOPENING: the question has been considered duplicate, but I'm not entirely convinced: here this question is roughly "what is the most Pythonic way to find an element in a list". And the first answer to the question is really extensive in all Python ways to do this. More on stackoverflow.com
🌐 stackoverflow.com
October 14, 2017
Help with .index()? finding multiple instances of item
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself. More on reddit.com
🌐 r/learnpython
9
3
March 11, 2023
Why doesn't python lists have a .find() method?
It actually exists for all iterators, its just called next. next((i for i in users if i['id'] == 2), None) More on reddit.com
🌐 r/Python
55
42
May 6, 2022
How to find all occurrences of a substring in a string while ignore some characters in Python?
You could use re for this. ex import re long_string = 'this is a t`es"t. Does the test work?' small_string = "test" chars_to_ignore = ['"', '`'] print(re.findall(f"[{''.join(chars_to_ignore)}]*".join(small_string), long_string)) More on reddit.com
🌐 r/learnpython
6
1
July 25, 2024
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
1 month ago - Use list.index("target") to get the position of the first match. Wrap it in a try/except ValueError block, since it raises an error when the string is not there. To find all matching positions, use a list comprehension with enumerate(): [i for i, val in enumerate(my_list) if val == "target"].
🌐
datagy
datagy.io › home › python posts › python lists › python: find list index of all occurrences of an element
Python: Find List Index of All Occurrences of an Element • datagy
December 30, 2022 - One of the most basic ways to get the index positions of all occurrences of an element in a Python list is by using a for loop and the Python enumerate function. The enumerate function is used to iterate over an object and returns both the index ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-the-indices-of-all-occurrences-of-an-element-in-a-list
Get the indices of all occurrences of an element in a list - Python - GeeksforGeeks
July 23, 2025 - List comprehension allows for a concise and efficient way to find indices. By iterating through the list enumerate() we can collect the indices where the element matches. The indices are stored in the list of indices. Python · a = [1, 2, 3, 2, 4, 2, 5] x = 2 ind = [i for i, val in enumerate(a) if val == x] print(ind) Output ·
🌐
Keploy
keploy.io › home › community › find elements in a python list: 7 methods with code examples
Find Elements in a Python List: 7 Methods with Code Examples | Keploy Blog
April 4, 2026 - Master element lookup in Python lists using in, index(), loops, and more. Learn with clear examples and optimize your search operations.
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-findall-in-python
re.findall() in Python - GeeksforGeeks
July 23, 2025 - python is cool." result = re.findall(r'python', s, re.IGNORECASE) print(result) ... Here we use a pattern that matches any number. The regular expression \d+ looks for one or more digits in a row. So if the text contains numbers like "12 apples and 5 bananas", it will find "12" and "5" and give us those as the result ... import re # Find all numbers in a string text = "There are 12 apples, 5 bananas, and 300 oranges."
Find elsewhere
🌐
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 - Use list comprehension and the enumerate() function to get indices of all occurrences of an item in a list · Lists are a built-in data type in Python, and one of the most powerful data structures.
🌐
30 Seconds of Code
30secondsofcode.org › home › dictionary › find matches
Python - Find matches in a list or dictionary - 30 seconds of code
August 20, 2024 - To find all keys in the provided dictionary that have the given value, you will use dictionary.items(), a generator and list() to return all keys that have a value equal to val. def find_keys(dict, val): return list(key for key, value in ...
Top answer
1 of 14
1828

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
281

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)
🌐
DEV Community
dev.to › keploy › python-find-in-list-a-comprehensive-guide-4263
Python Find in List: A Comprehensive Guide - DEV Community
November 19, 2024 - The skill of Python find in list is applied in real-world scenarios across various domains. Searching for User Data: In a list of user records, locate a specific user by ID. Filtering Data: Extract specific values based on conditions, such as finding all employees with salaries above a certain threshold.
🌐
Medium
medium.com › swlh › python-the-fastest-way-to-find-an-item-in-a-list-19fd950664ec
Python: The Fastest Way to Find an Item in a List | by Sebastian Witowski | The Startup | Medium
September 25, 2020 - If we don’t have a predefined ... to check all the numbers starting from 1), we might use a “while loop”. ... If yes, return it and stop the loop. Otherwise, check the next number · If we have a list of items that we want to check, we will use a “for loop” instead. I know that the number I’m looking for is smaller than 10 000, so let’s use that as the upper limit: Let’s compare both solutions (benchmarks are done with Python ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-find-indices-of-value-in-list
Python - Ways to find indices of value in list - GeeksforGeeks
July 11, 2025 - Let's explore some other methods on ways to find indices of value in list. ... The index() method can be used iteratively to find all occurrences of a value by updating the search start position.
🌐
Sentry
sentry.io › sentry answers › python › find item in list in python
Find item in list in Python | Sentry
2 weeks ago - 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. — November 15, 2023 ... David Y. — March 15, 2023 · Get the value of a DataFrame cell in Python Pandas
🌐
Reddit
reddit.com › r/learnpython › help with .index()? finding multiple instances of item
r/learnpython on Reddit: Help with .index()? finding multiple instances of item
March 11, 2023 -

Taken from https://www.programiz.com/python-programming/online-compiler/?ref=409055e9 :

vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of the first 'i' is returned

index = vowels.index('i')

print('The index of i:', index)

Output: The index of i: 2

Say that the list was much bigger, and you don't know the contents, but you know 'i' is in it more than once. What would be the best way to find all instances of 'i'?

Thanks so much! <3

Top answer
1 of 5
5
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself.
2 of 5
3
str.index() has parameters start and end, which can be used to define the starting index for the search. So, if you know that you have more than one substring in your string, you can do the following: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = test.index('i') // i_idx -> 12 i_idx = test.index('i', i_idx+1) // i_idx -> 16 You can use it in the loop, but note that the str.index() throws an exception if the substring is not found. So, you can loop until you stop finding the substring in your string like this: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while True: try: i_idx = test.index('i', i_idx+1) print(i_idx) except ValueError as e: print(e) break Output: 12 16 42 45 61 65 substring not found There is a sibling function, str.find(). It works the same, but it does not raise exception, instead it returns -1 when the substring is not found, so you can loop until the result of str.find() is -1 to find indices of all substring occurrences in your string: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while (i_idx := test.find('i', i_idx+1)) > 0: print(i_idx) Output: 12 16 42 45 61 65 There is a function str.count() which can be used in a loop as well: i_idx = 0 for _ in range(test.count('i')): i_idx = test.index('i', i_idx+1) print(i_idx) The disadvantages of all the above method is that you have a time complexity of O(m*n) for a string with length n and m occurences of your substring in your string, with or O(n**2) for the worst case. Better time complexity can be achieved if you simply iterate through the string only once: for idx, char in enumerate(test): if char == 'i': print(idx) or using comprehensions generator expression (as pointed out by u/kyber/ : indices = (idx for idx, char in enumerate(test) if char == 'i') print(*indices)
🌐
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)
April 22, 2026 - Python presents various methods for finding elements in lists, such as using the in operator, index method, count method, list comprehensions, and any and all functions. Each method or function can be used to find a particular item in lists.
🌐
Programiz
programiz.com › python-programming › methods › list › index
Python List index()
In this tutorial, we will learn about the Python List index() method with the help of examples.
🌐
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.
Published   April 27, 2025
🌐
Lucidar
lucidar.me › en › python › search-item-in-list
Find elements in lists in Python | Lulu's blog
October 11, 2021 - This page explains how to search elements in lists in Python. | Lulu's blog | Philippe Lucidarme