Python Not In Operator: The Complete Guide

To check if some element is not in the sequence, a list, string, tuple, or set, use the not in operator. The not in operator is the exact opposite of in operator.

The in operator in Python checks whether a specified value is a constituent element of a sequence.

Python not in operator

The not in is a built-in Python operator that checks the presence of a specified value inside the given sequence, but its values are opposite to that of the in operator. The not-in operator returns a boolean value as an output. It returns either True or False.

Python code for not in operator

listA = [11, 21, 29, 46, 19]
stringA = "Hello! This is AppDividend"
tupleA = (11, 22, 33, 44)

print(19 not in listA)
print("is" not in stringA)
print(55 not in tupleA)

Output

False
False
True

When you use the not in operator in a condition, the statement returns a Boolean result evaluating True or False.

In our example, first, we check the element from the list. It returns False because 19 is in the listA.

Then stringA contains the “is” as a substring, and that’s why False returns.

When the specified value is found inside the sequence, the statement returns True. Whereas when it is not found, we get a False.

Working of “in” and “not in” Operators in Dictionaries

Dictionaries are not sequences because dictionaries are indexed based on keys. Let’s see how to work with, not in operator in dictionaries? And if they do, how do they evaluate the condition?

Let us try to understand with an example.

dict1 = {11: "eleven", 21: "twenty one", 46: "fourty six", 10: "ten"}

print("eleven" in dict1)
print("eleven" not in dict1)

print(21 in dict1)
print(21 not in dict1)

print(10 in dict1)
print(10 not in dict1)

Output

False
True
True
False
True
False

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.