Python finding an element in a list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Python provides several ways to find elements in a list: the in operator for existence checks, list.index() for finding positions, list.count() for counting occurrences, list comprehensions for filtering multiple matches, and the filter() function for functional-style searching. For large datasets where frequent lookups are needed, converting to a set or dict provides O(1) lookups instead of O(n) linear scans.
Check Existence with in
The in operator returns True if the element exists anywhere in the list:
The in operator performs a linear scan — it checks each element from left to right until it finds a match or reaches the end. Time complexity is O(n).
Find Index with list.index()
list.index(value) returns the index of the first occurrence. It raises ValueError if the element is not found:
Find All Indices
Count Occurrences with list.count()
Note that list.count() always scans the entire list. For simple existence checks, in is more efficient because it stops at the first match.
Filter with List Comprehension
List comprehensions are the most Pythonic way to find elements matching a condition:
Find First Match with next()
To get only the first matching element without scanning the entire list:
The generator expression with next() is efficient because it stops iterating as soon as it finds the first match.
Using filter()
The filter() function applies a function to each element and returns an iterator of matches:
Performance: set and dict for Frequent Lookups
Lists have O(n) lookup time. For repeated membership checks, convert to a set for O(1) average-case lookups:
Common Pitfalls
- Using
list.index()without checking existence first:index()raisesValueErrorif the element is not in the list. Either use a try/except block or check withinbefore callingindex(). - Using
list.count()for existence checks:count()scans the entire list even if the element is found early. Useinfor simple existence checks, which stops at the first match. - Repeated
inchecks on large lists: Eachincheck is O(n). If you need to check membership thousands of times, convert the list to asetfirst for O(1) lookups. - Modifying a list while iterating over it: Removing elements during iteration skips elements or raises errors. Iterate over a copy (
for x in list(original)) or use list comprehension to build a new list. - Comparing objects without
__eq__: Custom objects use identity comparison (is) by default. Define__eq__on your class so thatinandindex()compare by value instead of by object identity.
Summary
- Use
infor simple existence checks (stops at first match, O(n)) - Use
list.index()to find the position of an element (raisesValueErrorif missing) - Use list comprehensions to find all matching elements or their indices
- Use
next()with a generator expression to efficiently find the first match - Convert to
setordictfor O(1) lookups when checking membership repeatedly
Related reading
- Python For each list element apply a function across the list
- Python for loops - for i in range0,lenlist vs for i in list
- Python gcd for list
- Python Graph Library
- Python Flask, how to set content type
- Python float argument must be a string or a number, not ''pandas._libs.interval.Interval''
- Python How to group a list of objects by their characteristics or attributes?
- python how to identify if a variable is an array or a scalar

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.