Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Check if an Element Is Not in a List in Python

Check whether a value is absent from a Python list with the not in operator, and understand membership, identity, dictionary behavior, and set trade-offs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How 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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.