October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check Whether a Python List or Array Contains a Value

Use Python’s in operator to test list membership, and choose values(), .any(), or .all() when your question is about dictionary values or array conditions.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a Python list, use value in values. It evaluates to True when the value is a member and False otherwise. Use not in for the inverse result.

Check whether a list contains a value

Python’s in and not in operators test membership. For built-in sequences such as lists and tuples, a match is found when an element is identical to the searched value or equal to it.

As an Amazon Associate I earn from qualifying purchases.

values = [10, 42, 99]

if 42 in values:
    print("found")

The condition is True, so the example prints found. To test for absence, write 42 not in values.

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

See the Python language reference for the membership operators and their semantics.

How membership differs by container

The same syntax can ask different membership questions depending on the container. In a dictionary, in checks keys, not values:

record = {"name": "Ada", "role": "engineer"}

"name" in record           # True: checks keys
"Ada" in record.values()   # True: checks values

If you need to perform repeated membership checks, a set or dictionary may be a more suitable structure when its membership semantics fit your task. Choose based on what you need to store and check; the syntax alone does not make these containers interchangeable.

Python arrays and elementwise conditions

For a NumPy array, value in array_values asks whether the scalar is a member. NumPy documents ndarray.__contains__ as returning bool(key in self).

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

That differs from asking whether one or all elements meet a condition. A comparison such as array_values > 10 produces elementwise Boolean results. Reduce those results explicitly:

(array_values > 10).any()  # True if at least one element is greater than 10
(array_values > 10).all()  # True if every element is greater than 10

NumPy warns that testing the truth value of a multi-element array is ambiguous and raises an error; use .any() or .all() to express which result you want. For an elementwise equality question, use (array_values == value).any().

See NumPy’s ndarray reference for membership and array truth-value behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What happens with a custom container

A custom Python object can define its membership behavior with __contains__(). If it has no such method, Python tries iteration, then the legacy indexed-sequence protocol. The Python data model documentation describes this fallback order.

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

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.