Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 07

Membership Operators (`in` / `not in`)

Check whether a value exists in an iterable with in and not in. Strings use substrings; dicts check keys by default.

Beginner18 min

Membership operators check whether a value exists inside an iterable.

An iterable is a collection that can hold multiple values, such as a string, list, tuple, set, or dictionary.

Letter P found as a substring in PYTHON

What you'll learn

  • Use in and not in with strings, lists, dictionaries, and sets
  • Understand that strings search for substrings
  • Learn that dictionaries check keys by default
  • Avoid the common nested-list misconception

Membership operators

Operator Meaning
in Returns True if the value exists
not in Returns True if the value does not exist
python
value in iterable
value not in iterable

Important: The container must be an iterable.

Iterable Non-iterable
str int
list float
tuple bool
set complex
dict —

Strings: search for substrings

Strings look for a continuous sequence of characters.

python
s = "PYTHON"

print("P" in s)
print("PYT" in s)
print("HON" in s)
print("PTO" in s)
text
True
True
True
False

"PTO" fails because those letters are not consecutive in "PYTHON".

Interactive string search

Try different substrings in "PYTHON".

"P" in "PYTHON"

true

"P" appears as a consecutive substring.

Interactive list search

lst = [10, "Rossum", 34.56] (+ 2+3j in the full lesson playground)

"34.56" in lst

true

This value is a whole list element.

Interactive dictionary search

Keys: 10, 20, 30 → values: Apple, Mango, Kiwi

"Apple" in d

false

"Apple" in d.values()

true

Membership on a dict checks keys by default. Use .values() to search values.

Strings with slicing

Membership also works on slices.

python
s = "PYTHON"

print("PTO" in s[::2])
print("NOH" in s[::-1])
text
True
True

Why?

  • s[::2] → "PTO"
  • s[::-1] → "NOHTYP"

Lists

Lists search for complete elements, not partial text.

python
lst = [10, "Rossum", 34.56, 2 + 3j]

print(34.56 in lst)
print("Ross" in lst)
print("Ross" in lst[1])
text
True
False
True

"Ross" is not a list element, but it is a substring of "Rossum".

Dictionaries

By default, membership checks keys, not values.

python
d = {
    10: "Apple",
    20: "Mango",
    30: "Kiwi",
}

print(10 in d)
print("Apple" in d)
text
True
False

To search values, use .values().

python
print("Apple" in d.values())
text
True

Sets

Sets search for individual elements.

python
vowels = {"a", "e", "i", "o", "u"}

print("a" in vowels)
print("x" in vowels)
text
True
False

A set does not treat another set as one of its elements unless that set is actually stored inside it.

The nested list trap

Many beginners expect this to be True:

python
[10, 20, 30] in [10, 20, 30, 40, 50]

Actual result: False

Why? Python searches for one element. The outer list contains 10, 20, 30, 40, and 50 — not the list [10, 20, 30] as a single element.

Summary table

Container in checks
String Consecutive substring
List Whole element
Tuple Whole element
Set Whole element
Dictionary Keys
dict.values() Values
dict.items() Key–value pairs

Practice exercises

Exercise 1 — Predict the result:

python
"HON" in "PYTHON"

Answer: True

Exercise 2 — Predict:

python
"Apple" in {10: "Apple"}

Why is the answer not True? (Keys are tested — 10 is a key, "Apple" is a value.)

Exercise 3 — Explain why this returns False:

python
[1, 2] in [1, 2, 3]

The inner [1, 2] is not a single element of the outer list.

Quick knowledge check

  1. What does in return when the value is missing?
    False

  2. Does in on a dictionary check values?
    No. It checks keys by default.

  3. Is "PTO" in "PYTHON" True?
    No. The letters are not consecutive in that order.

Summary

  • in and not in work with iterables.
  • Strings search for consecutive substrings.
  • Lists, tuples, and sets search for whole elements.
  • Dictionaries check keys unless you use .values() or .items().
  • A smaller list or set is not automatically inside a larger one.

Next lesson: Identity Operators (is / is not) — value equality (==) vs object identity (is).

Loading playground…