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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For a Python list, use value in list_name. The expression returns True when the value is a member and False otherwise. Use not in to test for absence.

Check whether a value is in a Python list

The membership operators work directly in conditions and return a Boolean result:

values = [10, 42, 99]

if 42 in values:
    print("found")

if 7 not in values:
    print("7 is absent")

Python’s language reference defines in and not in as membership tests. For built-in sequences such as lists and tuples, a value matches when an element is identical to it or equal to it. Python expression reference

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

What does in check for different containers?

The syntax is similar across built-in containers, but the membership question depends on the container type.

Container Membership check What it tests
List or tuple value in items Whether an element is identical to or equal to value.
Set value in items Whether the value is a set member.
Dictionary value in mapping Whether the value is a key, not a dictionary value.

To search dictionary values instead of keys, call values():

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

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

If your program performs repeated membership checks, a set or dictionary may be a more suitable structure than a list when its semantics fit. This is a data-structure choice; the appropriate container depends on how the data is used.

How does membership work for custom containers?

A custom class can define its membership behavior with __contains__(). If it does not, Python tries iteration; if iteration is unavailable, it falls back to the legacy indexed-sequence protocol. The details are documented in Python’s data model reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does in work with NumPy arrays?

NumPy supports scalar membership syntax: its ndarray.__contains__ method returns the Boolean result of key in self. For example, 42 in array_values asks whether the scalar value is present. NumPy ndarray reference

That is different from testing a condition against each element. A comparison such as array_values > 10 produces Boolean values; reduce them explicitly to ask whether any or all elements meet the condition:

(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 using a multi-element array directly as a truth value is ambiguous and raises an error. Use .any() or .all() to specify which result you mean. NumPy ndarray reference

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.