Membership Operators

Membership operators in Python are used to test whether a value exists inside a sequence or collection. They are commonly used with strings, lists, tuples, sets, and dictionaries.
Python provides two membership operators:
  • in
  • not in

What Are Membership Operators?

Membership operators check presence or absence of a value in a container.
They always return a Boolean value: True or False.
python
x = "a"
print(x in "apple")

Membership Operators Table

OperatorDescription
inReturns True if value exists in the sequence
not inReturns True if value does not exist in the sequence

Membership Operators with Strings

Checks if a substring or character exists in a string.
python
print("a" in "apple")
python
print("pp" in "apple")
python
print("z" not in "apple")

Membership Operators with Lists

Checks if an element exists in a list.
python
numbers = [1, 2, 3, 4, 5]
print(3 in numbers)
python
print(10 in numbers)
python
print(6 not in numbers)

Membership Operators with Tuples

python
colors = ("red", "green", "blue")
print("green" in colors)
python
print("yellow" not in colors)
python
print("red" in colors)

Membership Operators with Sets

Sets are optimized for fast membership checks.
python
items = {10, 20, 30}
print(20 in items)
python
print(40 not in items)
python
print(10 in items)

Membership Operators with Dictionaries (Tricky)

Membership checks in dictionaries work on keys, not values.
python
student = {"name": "Jayesh", "age": 25}

print("name" in student)
python
print("Jayesh" in student)
python
print("age" not in student)

Checking Values Explicitly

python
print("Jayesh" in student.values())
python
print(25 in student.values())

Membership Operators in if Conditions

python
email = "user@gmail.com"

if "@" in email:
    print("Valid email")
python
password = "admin123"

if "@" not in password:
    print("Missing special character")

Membership Operators with Nested Collections

python
data = [[1, 2], [3, 4], [5, 6]]
print([3, 4] in data)
python
print(3 in data)
python
print(6 not in data)

Case Sensitivity in Membership

Membership checks are case-sensitive.
python
print("Python" in "python programming")
python
print("python" in "python programming")

Performance Insight (Important)

  • in on sets and dictionaries is faster than lists
  • Lists require linear search
  • Sets and dicts use hashing
python
print(5 in [1, 2, 3, 4, 5])
python
print(5 in {1, 2, 3, 4, 5})

Common Mistakes

Mistake 1: Assuming Dictionary Checks Values

python
data = {"a": 1, "b": 2}
print(1 in data)
This checks keys, not values ❌

Mistake 2: Expecting Partial Match in Lists

python
words = ["apple", "banana"]
print("app" in words)
This returns False.

Summary

  • Membership operators test presence or absence
  • Python provides in and not in
  • Work with strings, lists, tuples, sets, dictionaries
  • Dictionary membership checks keys only
  • Case-sensitive by default
  • Sets are fastest for membership tests

Practice

  • Check if a word exists in a sentence
  • Validate email using membership operators
  • Check presence of a key in a dictionary
  • Compare membership performance between list and set