How to Check if a List Contains a Specific Item
To check if a list contains a specific item, you can use the in
keyword.
Example: Checking if a List Contains an Item
# Checking if a list contains an item
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Banana is in the list.")
else:
print("Banana is not in the list.")
Output
Banana is in the list.
Explanation: The in
keyword checks if the specified item exists in the list and returns True
or False
. If 'banana' is in the list, the condition will be true, and the message will be printed.