The in operator
Often you need to know whether a list holds some value: is this name on the guest list, is this item in the cart? x in my_list answers that. It compares each item with x, from the first one on, and gives True as soon as one matches or False if none does. x not in my_list is the opposite: it is True when x is missing.
bag = ["map", "water", "snacks"]
print("water" in bag)
print("tent" in bag)
print("tent" not in bag)
True
False
True
The answer is a bool, so it fits straight into an if.
Traps
- It compares values, not indexes.
2 in ["a", "b", "c"]isFalse, even though index 2 exists. - Text must match exactly. With the bag above,
"Water" in bagisFalsebecause the list holds"water". - On a string,
inlooks for a piece of text."map" in "mapping"isTrue. On a list, it only matches whole items.
The lesson's task
The list bag is ["map", "water", "snacks"]. The task has two independent checks, so it uses two separate if statements.
First, if "water" is in bag, print Water is packed. The bag does hold water, so the condition is true and the print runs. Second, if "tent" is not in bag, print Pack a tent. There is no tent, so not in gives True and this print runs too.
bag = ["map", "water", "snacks"]
if "water" in bag:
print("Water is packed")
if "tent" not in bag:
print("Pack a tent")
Water is packed
Pack a tent
Using if twice rather than if and else matters here: the two questions are about different items, so both can be true, as they are in this task.
Finding where, or counting
in only says yes or no. When you need the position of a value, my_list.index(x) gives the index of the first match, but it raises a ValueError when x is missing, so check with in first if you aren't sure. my_list.count(x) says how many times x appears, and gives 0 when it doesn't.
nums = [1, 2, 2, 3]
print(2 in nums)
print(nums.index(3))
print(nums.count(2))
print(9 in nums)
print(nums.index(9))
True
3
2
False
ValueError: 9 is not in list
Numbers follow the same rule as other values: 2 in [1, 2, 3] is True, while "2" in [1, 2, 3] is False because the text "2" is not the number 2. An empty list holds nothing, so "x" in [] is False.
Common mistakes
- Writing the question backward. The item goes on the left and the list on the right:
"water" in bag. The other way round,bag in "water", asks whether the whole list is a piece of the text"water", and Python stops with aTypeError. - Using the wrong direction for the second check. The task asks to pack a tent when it is missing. Writing
"tent" in bagwould print nothing here, because the bag has no tent. - Adding an
elsethat isn't asked for. The task prints one line per check only when its condition holds. Extra branches print extra lines, and the output no longer matches.
Checking a list item by item is fine for short lists like these. Later, when a collection gets large or you run the check many times, a set or a dictionary answers the same question much faster.
Key takeaways
x in my_listisTruewhen some item equalsx;not inreverses it.- Matching is by value and exact, including upper and lower case.
- Use it as the condition of an
if.
