Python Membership Operators “in” and “not in” Explained

When checking whether a particular value is part of a sequence, you may want to use the “in” or “not in” operators in Python.

What are “in” and “not in” operators in Python?
Also known as membership operators, the “in” and “not in” Python operators are used when you need to check whether a particular value is part of a sequence. That sequence can be a list, array, string, tuple, or any other iterable. If the given element is found, the “in” operator returns True; otherwise, it returns False. The “not in” operator returns the opposite. If the given element is not found, the “not in” operator returns True; otherwise, it returns False.

In the following examples, I’ll show you how to use the “in” operator and its negation “not in” operator.

Python “in” operator

The most common example of using the “in” operator in Python is checking if a particular value or object is part of a list or if a specific character is part of a string. It returns a Boolean value, which depends on how the “in” statement was evaluated.

The “in” operator is used in the following format:
X in Y
This statement checks whether the X value is part of the Y sequence.

To illustrate how the Python “in” operator works, look at the following examples.

list_numbers = [1, 2, 3, 4, 5]
print(1 in list_numbers)  # True
print(10 in list_numbers)  # False

tuple_strings = ("abc", "def", "ghi")
print("abc" in tuple1)  # True
print("jkl" in tuple1)  # False

string_example = "Learning how in and not in operators work"
print("operators" in string_example)  # True
print("abc" in string1)  # False

As you can see, we used the Python “in” operator with lists, tuples and strings. Each of these sequences allows checking if the value is one of the members.

Sometimes it can be confusing if you’re using the “in” operator with the dictionary since it contains key-value pairs. In that case, the “in” operator checks whether the specified value exists as a key in a dictionary. In our example, keys are 1 and 2.

dict_example = {
  1: "Value1", 
  2: "Value2"
}
print(1 in dict_example)  # True
print("Value1" in dict_example)  # False

If you want to check if the value exists in a dictionary use the values() method to retrieve all values from a dictionary. Values are returned as a list, so now you can use the “in” operator as you would with any other list.

dict_example = {
  1: "Value1", 
  2: "Value2"
}
print(1 in dict_example.values())  # False
print("Value1" in dict_example.values())  # True

Python “not in” operator

When paired with the “not” keyword, you get the opposite result of using only the “in” operator. What the “in” operator would return as True, the “not in” Python operator will return as False. The same applies the other way around. 

So if you wanted to check whether a specific value is not a part of the sequence, you’d write it like this:
X not in Y
This statement checks whether the X value is not part of the Y sequence.

Now to illustrate it, I’ll use the same examples as before.

list_numbers = [1, 2, 3, 4, 5]
print(1 not in list_numbers)  # False
print(10 not in list_numbers)  # True

tuple_strings = ("abc", "def", "ghi")
print("abc" not in tuple1)  # False
print("jkl" not in tuple1)  # True

string_example = "Learning how in and not in operators work"
print("operators" not in string_example)  # False
print("abc" not in string1)  # True

In the above examples, we again used the Python “not in” operator with lists, tuples and strings. Note that the evaluation of the “not in” operator over these examples resulted in opposite Boolean values than those obtained using the “in” operator.

The same logic applies to dictionaries. If the key is not present, the “not in” operator will return True. If the key is present, the “not in” operator will return False.

dict_example = {
  1: "Value1", 
  2: "Value2"
}
print(1 not in dict_example)  # False
print("Value1" not in dict_example)  # True

If you want to check if the value does not exist in the dictionary, retrieve the list of values, then use the “not in” operator. It will return True if the value does not exist in the dictionary, otherwise False.

dict_example = {
  1: "Value1", 
  2: "Value2"
}
print(1 not in dict_example.values())  # True
print("Value1" not in dict_example.values())  # False

Best practices when using the Python “not in” and “in” operators

Use the “X not in Y” instead of “not X in Y”

Although the “X not in Y” is logically the same as the “not X in Y” statement, I recommend you to use the “X not in Y” always. It’s more readable, and if you work with other devs on the same code it will be easier for everyone to understand the statement.

Define __contains__() function for complex sequences

The “in” and “not in” operators are supported by types that are iterable or implement the __contains__() method. So, if you want to define your way of comparing objects inside a sequence, implement the __contains__() method. This will enable you to use the “in” and “not in” operators even for complex objects.