Python In Operator: Effortlessly Check if an Element Exists in a List
Python’s in
and not in
Operators: Check for Membership
Python’s in
and not in
operators allow you to quickly determine if a given value is or isn’t part of a collection of values. This type of check is common in programming, and it’s generally known as a membership test in Python. Therefore, these operators are known as membership operators.
Getting Started With Membership Tests in Python
Sometimes you need to find out whether a value is present in a collection of values or not. In other words, you need to check if a given value is or is not a member of a collection of values. This kind of check is commonly known as a membership test.
Arguably, the natural way to perform this kind of check is to iterate over the values and compare them with the target value. You can do this with the help of a for
loop and a conditional statement.
Consider the following is_member()
function:
This function takes two arguments, the target value
and a collection of values, which is generically called iterable
. The loop iterates over iterable
while the conditional statement checks if the target value
is equal to the current value. Note that the condition checks for object identity with is
or for value equality with the equality operator (==
).
While the above function works, Python provides a more elegant and efficient way to perform membership tests using the in
and not in
operators.
Python’s in
Operator
The in
operator is used to check if a value is present in a collection of values. It returns True
if the value is found, and False
otherwise.
Here’s an example of using the in
operator with a list of integers:
In this example, the first in
expression checks if the value 3
is present in the list numbers
. Since 3
is in the list, the expression evaluates to True
. The second expression checks if the value 6
is present in the list, but since it is not, the expression evaluates to False
.
You can also use the in
operator with other types, such as strings, tuples, sets, and dictionaries.
Python’s not in
Operator
The not in
operator is the negation of the in
operator. It checks if a value is not present in a collection of values. It returns True
if the value is not found, and False
otherwise.
Let’s continue with the previous example and use the not in
operator:
In this case, the first not in
expression checks if the value 3
is not present in the list numbers
. Since 3
is in the list, the expression evaluates to False
. The second expression checks if the value 6
is not present in the list, and since it is not, the expression evaluates to True
.
Using in
and not in
With Different Python Types
You can use the in
and not in
operators with different Python data types. Let’s explore some examples:
Lists, Tuples, and Ranges
Strings
Generators
Dictionaries and Sets
Conclusion
Python’s in
and not in
operators provide a convenient and efficient way to perform membership tests. You can use them with various data types, including lists, tuples, strings, dictionaries, sets, ranges, and even generators. By using these operators, you can determine whether a value is or isn’t part of a collection of values, which is a common operation in programming.