Start learning free

LESSON 113 OF 673 · LISTS I

Checking a List for a Value in Python with in

Lesson Read the full explanation

TL ; DR:

x in my_list is True when the list holds x, and False when it doesn't. Use it as the condition of an if.


colors = ["red", "green", "blue"]

print("red" in colors)    # True
print("pink" in colors)   # False

if "pink" not in colors:
    print("No pink yet")


Full lesson:

Often you need to know if 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. It gives True as soon as one matches, or False if none does.

Use x not in my_list for the opposite. It gives True when x is missing.

The answer is a bool, so it fits right into an if:

cart = ["milk", "bread"]

if "milk" in cart:
    print("Milk is in the cart")

It compares values, not indexes. So 2 in ["a", "b", "c"] is False, even though index 2 exists.

Text must match exactly. So "Red" in colors is False, because the list has "red".


Your task

bag holds what is packed for a hike.

If "water" is in bag, print Water is packed. If "tent" is not in bag, print Pack a tent.

Expected output:

Water is packed
Pack a tent
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · LISTS I

    Array Membership in Python, explained

    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"] is False, even though index 2 exists.
    • Text must match exactly. With the bag above, "Water" in bag is False because the list holds "water".
    • On a string, in looks for a piece of text. "map" in "mapping" is True. 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 a TypeError.
    • Using the wrong direction for the second check. The task asks to pack a tent when it is missing. Writing "tent" in bag would print nothing here, because the bag has no tent.
    • Adding an else that 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_list is True when some item equals x; not in reverses it.
    • Matching is by value and exact, including upper and lower case.
    • Use it as the condition of an if.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free