Intern
How to check if a value exists in a list in Python?
sobes.tech AI
Answer from AI
Using the in operator.
# List example
my_list = [1, 2, 3, 4, 5]
# Value to check
value_to_check = 3
# Existence check
if value_to_check in my_list:
print(f"{value_to_check} exists in the list.")
else:
print(f"{value_to_check} does not exist in the list.")
# Another example
another_value = 10
if another_value in my_list:
print(f"{another_value} exists in the list.")
else:
print(f"{another_value} does not exist in the list.")