Skip to content

Python In Operator: Effortlessly Check if an Element Exists in a List

CodeMDD.io

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:

def is_member(value, iterable):
for item in iterable:
if value is item or value == item:
return True
return False

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:

numbers = [1, 2, 3, 4, 5]
print(3 in numbers) # Output: True
print(6 in numbers) # Output: False

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:

numbers = [1, 2, 3, 4, 5]
print(3 not in numbers) # Output: False
print(6 not in numbers) # Output: True

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

fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits) # Output: True
print('orange' not in fruits) # Output: True
numbers = (1, 2, 3, 4, 5)
print(3 in numbers) # Output: True
print(6 not in numbers) # Output: True
weekdays = range(7)
print(2 in weekdays) # Output: True
print(8 not in weekdays) # Output: True

Strings

message = "Hello, World!"
print('World' in message) # Output: True
print('Python' not in message) # Output: True

Generators

evens = (num for num in range(10) if num % 2 == 0)
print(4 in evens) # Output: True
print(5 not in evens) # Output: True

Dictionaries and Sets

person = {'name': 'Alice', 'age': 25, 'location': 'New York'}
print('age' in person) # Output: True
print('email' not in person) # Output: True
characters = {'Mario', 'Luigi', 'Peach'}
print('Bowser' not in characters) # Output: True
print('Luigi' in characters) # Output: True

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.

CodeMDD.io