How to check if something is not in a list in Python
Codemia Knowledge Hub ·
Use the not in operator: if x not in items:. It returns True when no element of the list is equal to x. Two things affect whether the check is correct and fast: how your values compare for equality, and whether a list (linear scan) or a set (hash lookup) holds them.
Basic syntax
The operator returns True if no equal element exists in the sequence.
Case-insensitive checks
String membership is case-sensitive, so "alice" is not found in a list containing "Alice". When your rules are case-insensitive, normalize both the source and the query:
List vs set performance
Membership in a list is linear (O(n)), while set membership is average constant time (O(1)):
If you run many checks, especially in a hot path, convert the list to a set once and check against that.
Custom objects and equality
Membership relies on equality (__eq__), and on hashing (__hash__) for sets. Instances of a custom class do not compare by value unless you implement __eq__, and a bad implementation makes the check wrong:
Mixing incompatible types, such as searching a list of integers for the string "2", means in never finds a match and not in is always True, which hides the real bug.
Guard clauses
not in works well for defensive early exits:
This keeps validation concise and readable. Avoid mutating a collection while you iterate over it and check membership in it at the same time.
Validating code that relies on these checks
A correct snippet is only the start when a membership check guards real input. Decide what "working" means before you call it done: one correctness measure (for example, output agreement with known-good fixtures), one performance measure (for example, p95 runtime under expected load), and, if retries are involved, one reliability measure such as failure rate.
Keep a short executable checklist in version control and run it in CI where possible: validate environment assumptions, run a minimal happy-path example, run one malformed-input case, and confirm the logs carry enough context for troubleshooting. If external systems are involved, add a dry-run mode that exercises integration paths without destructive actions. A typical validation flow:
Also make ownership explicit: who responds when the component fails, what alert threshold triggers an investigation, and what fallback is acceptable, such as disabling a feature flag or reverting one deployment. Finally, document what the approach handles and what it does not try to solve, so it is not misused later.
Related reading
- Check if sum possible in array
- Check if two arrays are cyclic permutations
- Check if two linked lists merge. If so, where?
- Check if two unordered lists are equal
- Check if string ends with one of the strings from a list
- Check if the digits in the number are in increasing sequence in python
- Check if value already exists within list of dictionaries in Python?
- Check RabbitMQ queue size from client
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.