Python provides the not in
operator in order checks if the specified item is not in the specified list, tuple. The “not in” operator is generally used with the if sentences in order to check if the specified item exists in the specified list.
Check If Specified Variable Exists In List
A variable value can be checked if it exists in a list. In the following example, we check if variable named ankara
exists in the list named cities
.
ankara = "ankara"
cities = ["istanbul","ankara","izmir"]
if ankara not in cities:
print("ankara exist in cities")
else:
print("ankara does not exist in cities")
Check If Specified Value Exists In List
A value or item can be checked if it exists in a list. In the following example, we check if value ankara
exists in the list named cities
.
cities = ["istanbul","ankara","izmir"]
if "ankara" not in cities:
print("ankara exist in cities")
else:
print("ankara does not exist in cities")
Check If Specified Variable Exists In Tuple
The “not in” operator can be also used with the tuple type. The usage is very same as the list type which is described above. In the following example, we check if the variable named ankara
exist in the tuple named cities
.
ankara = "ankara"
cities = ("istanbul","ankara","izmir")
if ankara not in cities:
print("ankara exist in cities")
else:
print("ankara does not exist in cities")
Check If Specified Value Exists In Tuple
In the following example, we check if the value ankara
exist in the tuple named cities
.
cities = ("istanbul","ankara","izmir")
if "ankara" not in cities:
print("ankara exist in cities")
else:
print("ankara does not exist in cities")