How to Check if a List Contains a Specific Element
In Python, you can use the in
operators to check whether a specific element exists in a list.
To check whether an element is not in a list, use the not in
operator.
in Operator
The in
operator checks if a specific element is included in a list.
It returns True
if the element exists in the list, and False
if it does not.
Example of in Operator
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("orange" in fruits) # False
not in Operator
The not in
operator checks if a specific element is not included in a list.
It returns True
if the element is not in the list, and False
if it is.
Example of not in Operator
fruits = ["apple", "banana", "cherry"]
print("orange" not in fruits) # True
print("apple" not in fruits) # False
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.