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.
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.
What you'll learn
- Use
inandnot inwith 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 |
value in iterable
value not in iterableImportant: 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.
s = "PYTHON"
print("P" in s)
print("PYT" in s)
print("HON" in s)
print("PTO" in s)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.
s = "PYTHON"
print("PTO" in s[::2])
print("NOH" in s[::-1])True
TrueWhy?
s[::2]→"PTO"s[::-1]→"NOHTYP"
Lists
Lists search for complete elements, not partial text.
lst = [10, "Rossum", 34.56, 2 + 3j]
print(34.56 in lst)
print("Ross" in lst)
print("Ross" in lst[1])True
False
True"Ross" is not a list element, but it is a substring of "Rossum".
Dictionaries
By default, membership checks keys, not values.
d = {
10: "Apple",
20: "Mango",
30: "Kiwi",
}
print(10 in d)
print("Apple" in d)True
FalseTo search values, use .values().
print("Apple" in d.values())TrueSets
Sets search for individual elements.
vowels = {"a", "e", "i", "o", "u"}
print("a" in vowels)
print("x" in vowels)True
FalseA 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:
[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:
"HON" in "PYTHON"Answer: True
Exercise 2 — Predict:
"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:
[1, 2] in [1, 2, 3]The inner [1, 2] is not a single element of the outer list.
Quick knowledge check
-
What does
inreturn when the value is missing?
False -
Does
inon a dictionary check values?
No. It checks keys by default. -
Is
"PTO" in "PYTHON"True?
No. The letters are not consecutive in that order.
Summary
inandnot inwork 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…