Another way:

>>> [i for i in range(len(a)) if a[i] > 2]
[2, 5]

In general, remember that while find is a ready-cooked function, list comprehensions are a general, and thus very powerful solution. Nothing prevents you from writing a find function in Python and use it later as you wish. I.e.:

>>> def find_indices(lst, condition):
...   return [i for i, elem in enumerate(lst) if condition(elem)]
... 
>>> find_indices(a, lambda e: e > 2)
[2, 5]

Note that I'm using lists here to mimic Matlab. It would be more Pythonic to use generators and iterators.

Answer from Eli Bendersky on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-if-any-element-in-list-satisfies-a-condition
Check if any element in list satisfies a condition-Python - GeeksforGeeks
July 12, 2025 - It stops as soon as it finds the first matching element and returns True otherwise, it returns False. filter() applies a condition to each element and returns an iterator of matching values.
Discussions

How can I find a value in a list using Python? - Ask a Question - TestMu AI Community
I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list? More on community.testmuai.com
🌐 community.testmuai.com
0
June 3, 2024
How to check if elements in a list meet a specific condition using the 'any' and 'all' functions
additionally, all will report True for an empty list, in accordance with how mathematicians think of it (aka correctly). you can think of it as "is there not an exception?" another way of looking at it is that the and of two all statements must be the same as the all on the combined lists. e.g.: all([1,2,3]) and all([]) == all([1,2,3] + []) therefore all([]) must be True. by similar logic, any reports False to an empty list. More on reddit.com
🌐 r/pythontips
3
7
March 22, 2024
How to print specific elements from a list that meet a certain condition?
Hi - i’m basically stuck and my class lecture slides and textbook is not helping. I have “a” as my list of numbers. I got it to print anything that’s less than or equal to 34 but need to also print if it’s 89. I can’t seem to get the 89 in. I’ve tried: for n in a: print(n) if ... More on discuss.python.org
🌐 discuss.python.org
0
February 28, 2024
python - find first element and index matching condition in list - Stack Overflow
In the code above, I loop over ... to find the correct elements. This looks very cumbersome to me. Is there a better way to do this? Using a native python function for instance? ... The approach described in the question (imperative for-loop) has a clear advantage of breaking the iteration as soon as the condition is met and doesn't require intermediary data structures in memory. The list-comprehension ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How can I find a value in a list using Python? - Ask a Question - TestMu AI Community
June 3, 2024 - I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list?
🌐
StrataScratch
stratascratch.com › blog › how-to-get-the-index-of-an-item-in-a-list-in-python
How to Get the Index of an Item in a List in Python - StrataScratch
September 6, 2024 - That is why, in this article, we’ll guide you through the simplicity of finding items in lists by using Python’s index() tool and discover some less basic techniques the function can master for you in more customized circumstances. A list in Python is a very useful container that can hold different types of items, such as numbers, strings, or even other lists. It works like a container of other elements, a flexible box allowing you to group several items.
🌐
Reddit
reddit.com › r/pythontips › how to check if elements in a list meet a specific condition using the 'any' and 'all' functions
r/pythontips on Reddit: How to check if elements in a list meet a specific condition using the 'any' and 'all' functions
March 22, 2024 -

Suppose you have a list of numbers, and you want to check if any of the numbers are greater than a certain value, or if all of the numbers are less than a certain value.

That can be done with this simple code:

# Original list
lst = [1, 2, 3, 4, 5]

# Check if any number is greater than 3
has_greater_than_3 = any(x > 3 for x in lst)

# Check if all numbers are less than 5
all_less_than_5 = all(x < 5 for x in lst)

# Print the results
print(has_greater_than_3)  # True
print(all_less_than_5)   # False

The 'any' function returns True if at least one element meets the condition, and the 'all' function returns True if all elements meet the condition.

🌐
Sentry
sentry.io › sentry answers › python › find item in list in python
Find item in list in Python | Sentry
The list comprehension in odd_numbers will go through my_list and construct a new list of every item it finds that has a nonzero modulo 2 (that is, produces a remainder when divided by two). If we want only the first item from our list that satisfies our condition, we change our list comprehension into a call to the Python next function.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › where
Python Numpy where() - Conditional Element Search | Vultr Docs
December 30, 2024 - The numpy.where() function is a versatile tool in the Python numpy library, primarily used to locate elements or indices in an array that meet certain conditions.
Find elsewhere
🌐
Real Python
realpython.com › python-first-match
How to Get the First Match From a Python List or Iterable – Real Python
May 28, 2023 - To find the first element matching a certain criteria in a list, you can add a conditional expression to the generator comprehension so the resulting iterator will only yield items that match your criteria.
🌐
Python.org
discuss.python.org › python help
How to print specific elements from a list that meet a certain condition? - Python Help - Discussions on Python.org
February 28, 2024 - Hi - i’m basically stuck and my class lecture slides and textbook is not helping. I have “a” as my list of numbers. I got it to print anything that’s less than or equal to 34 but need to also print if it’s 89. I can’t seem to get the 89 in. I’ve tried: for n in a: print(n) if n == 34: continue if n == 89: break HOWEVER, that prints all numbers in the list from 34-89.
🌐
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 - Essentially, you want to say: "If during the iteration, the value at the given position is equal to 'Python', add that position to the new list I created earlier". You use the append() method for adding an element to a list.
🌐
Analytics Vidhya
analyticsvidhya.com › home › check if element exists in list in python
Here's How You can Check if Element Exists in List in Python
Let us now look at methods to check if the element exists in this Python find element in list. The most straightforward way to check if an element exists in a list is by using the ‘in’ operator. This operator returns True if the element is found in the list and False otherwise.
Published   May 1, 2025
Top answer
1 of 3
4

Something like this should work:

Copyl = [-1,-2,3,4,5,6]
list(x > 0 for x in l).index(True)
# Output: 2

To find all patters, we can use python built in functions using

Copyfrom itertools import filterfalse
f = filterfalse(lambda x: x[1] <= 0, enumerate(l))
print(list(f))
# [(2, 1), (3, 2), (4, 3)]
2 of 3
2

You could do it in a list comprehension. This is basically the same as your code but condensed into one line, and it builds a list of results that match the criteria.

The first way gets all the matches

Copymylist = [-1,-2,3,4,5,6]

results = [(i, el) for i, el in enumerate(mylist) if el > 0]

Another way would be to use a generator expression which is probably faster, and just unpack it. This gets the first one.

Copy*next((i, el) for i, el in enumerate(mylist) if el > 0))

This loops the list and checks the condition, then puts the index and element into a tuple. Doing this inside parentheses turns it into a generator, which is much faster because it hasn't actually got to hold everything in memory, it just generates the responses as you need them. Using next() you can iterate through them. As we only use next() once here it just generates the first match. Then we unpack it with *

As there are two other valid answers here I decided to use timeit module to time each of them and post the results. For clarity I also timed the OP's method. Here is what I found:

Copyimport timeit
# Method 1 Generator Expression
print(timeit.timeit('next((i, el) for i, el in enumerate([-1,-2,3,4,5,6]) if el > 0)', number=100000))
0.007089499999999999

# Method 2 Getting index of True
print(timeit.timeit('list(x > 0 for x in [-1,-2,3,4,5,6]).index(True)', number=100000))
0.008104599999999997

# Method 3 filter and lambda
print(timeit.timeit('myidx , myel = list(filter(lambda el: el[1] > 0, enumerate([-1,-2,3,4,5,6])))[0]', number=100000))
0.0155314

statement = """
for idx, el in enumerate([-1,-2,3,4,5,6]):
    if el > 0:
        myidx, myel = idx, el
        break
"""

print(timeit.timeit(statement, number=100000))
0.04074070000000002
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-element-exists-in-list-in-python
Check if element exists in list in Python - GeeksforGeeks
a = [10, 20, 30, 40, 50] key = 30 flag = False for val in a: if val == key: flag = True break if flag: print("Element exists in the list") else: print("Element does not exist")
Published   November 13, 2025
🌐
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 - A loop provides a straightforward way to iterate through the list and collect indices of matching values. This is useful when conditions beyond equality are required.
🌐
Python Forum
python-forum.io › thread-34228.html
An IF statement with a List variable
I'm trying to make it so if i enter a 'Woman, girl or lady it will do the if statement isfemale = ['woman', 'girl', 'lady', ] isfemale = input('Enter your Gender: ') if isfemale == [0,1,2]: print('You are a Woman!')
🌐
Enterprise DNA
blog.enterprisedna.co › how-to-find-the-index-of-an-element-in-a-list-python-8-ways
How to Find the Index of an Element in a List Python: 8 Ways – Master Data Skills + AI
The generator expression (i for i, v in enumerate(my_list) if v search_for) creates a generator that will yield indices where the condition v search_for is True. The next() function is used to get the first item from this generator. The result is the index of the first occurrence of ‘banana’ ...
🌐
IronPDF
ironpdf.com › ironpdf blog › pdf tools › python-find-in-lists
Python Find in Lists (How It Works For Developers)
January 18, 2026 - Visual Studio Code: Install at least one development environment for Python. For the sake of this tutorial, we will consider the Visual Studio Code editor. The simplest way to check if an element exists in a list is by using the in operator.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-find-index-of-elements-that-meet-condition
Find the index of Elements that meet a condition in Python | bobbyhadz
Copied!my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com · On each iteration, we check if the current element meets a condition.