How to Check if a List Contains a Specific Element
You can use the in
operator to determine if 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.
The in
operator returns True
if the element is in the list, and False
if it is 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.