Use Python’s not in operator: if element not in my_list:. It evaluates to true when no list element is the same object as the value or compares equal to it.
Use not in to test list membership
Put the value you are checking on the left and the list on the right:
As an Amazon Associate I earn from qualifying purchases.
blocked = ["guest", "anonymous"]
username = "sam"
if username not in blocked:
print("Access may continue")
The condition runs when username is absent from blocked. Python defines not in as the inverse truth value of in. For built-in lists, membership succeeds when a list element is identical to the queried object or compares equal to it. See the Python language reference on membership tests.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchHow not in differs from is not
not in checks membership; is not checks object identity. For example, item is not items asks whether item and items are different objects—it does not check whether item occurs in the list. Use element not in my_list for the membership test. The Python tutorial distinguishes membership and identity tests.
#1 Best Overall
Use the right container’s membership rule
The same operator works with other containers, but what counts as a member depends on the container. With a dictionary, key not in mapping checks whether the key is missing; it does not search the values. To check values, use value not in mapping.values(). The language reference documents membership behavior.
When to use a set instead of a list
A list is straightforward for occasional checks, especially when you need to preserve order, duplicates, or values that cannot be placed in a set. Repeated membership checks may make a set worth considering if its semantics fit.
Rank #2
| Container | Membership cost in the CPython complexity table | Relevant behavior |
|---|---|---|
| List | O(n) | Search time grows linearly with list length. |
| Set | O(1) average case | Set elements must be hashable; duplicates collapse, and sets do not preserve insertion order or support indexing. |
These complexity figures come from the CPython time-complexity documentation; they are not benchmarks or a guarantee for every Python implementation, and set operations can degrade in the worst case. The built-in set documentation describes set behavior.
Free tools Windows power users keep installed
One-click scans. No signup required.
Before converting a list, check whether all its values are hashable and whether losing duplicates, ordering, or indexing would affect your code. A set is not a drop-in replacement when those list properties matter.
Use an explicit negation only when it helps readability
not (username in blocked) has the same truth value as username not in blocked, but the direct operator is the clearer idiom:
if username not in blocked:
print("Access may continue")
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Custom containers can define membership behavior
For a custom class, membership may be implemented through __contains__, iteration, or the legacy indexed-access protocol. If not in produces an unexpected result for a custom container, inspect how that class implements membership.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




