Let's see some examples to demonstrate how to check If an element is or is not in the List.
Example 1:
numbers = [1, 5, 2, 3, 9]
if 2 in numbers:
print("2 is present in the list")
else:
print("2 is not present in the list")
if 6 in numbers:
print("6 is present in the list")
else:
print("6 is not present in the list")
Output:
2 is present in the list
6 is not present in the list
Let's see another example with a list of strings
cities = ['London', 'Paris', 'Tokyo', 'New York', 'Sydney']
check_element_1 = 'Paris'
check_element_2 = 'Rome'
if check_element_1 in cities:
print(f"{check_element_1} is present in the list of cities")
else:
print("{check_element_1} is not present in the list of cities")
if check_element_2 in cities:
print(f"{check_element_2} is present in the list of cities")
else:
print(f"{check_element_2} is not present in the list of cities")
Output:
Paris is present in the list of cities
Rome is not present in the list of cities
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!