Python “in” and “not in” Membership Operators: Examples and Usage

Python Membership Operators Thumbnail

Python has many built-in operators to perform various operations, among them membership operators “in” and “not in” are used to check if something specific is present in iterable or not. Python uses these operators frequently to make search operations easier and more efficient.

This tutorial will introduce you to the “in” and “not in” membership operators and give you several examples so that you can easily implement them in your Python program.

Python “in” Operator

Python “in” operator is used to check whether a specified value is a constituent element of a sequence like string, array, list, tuple, etc.

When used in a condition, the statement returns a Boolean result evaluating either True or False. When the specified value is found inside the sequence, the statement returns True. Whereas when it is not found, we get a False.

Now let us take an example to get a better understanding of the working of “in” operator.

Example:

#in operator working

list1= [1,2,3,4,5]
string1= "My name is AskPython"
tuple1=(11,22,33,44)

print(5 in list1) #True
print("is" in string1) #True
print(88 in tuple1) #False

Output:

Python In Output
Python in Output

Explanation:

  • Here we have initialised a list of integers list1, a string string1 and a tuple tuple1 with some values. Then we use the ‘in’ operator to check whether a value is a part of the above sequences or not.
  • In the output, the integer 5 in list1 evaluates into a True, which signifies that the value 5 is found inside the list in Python.
  • Similarly, using the “in” operator we also confirm the presence of the string “is” in string1.
  • But for the last case, the condition results in a False since 88 does not exist inside the sequence tuple1.

Python “not in” Operator

The “not in” operator in Python works exactly the opposite way as the “in” operator. It also checks the presence of a specified value inside a given sequence but its return values are totally opposite to that of the “in” operator.

When used in a condition with the specified value present inside the sequence, the statement returns False. Whereas when it is not, we get a True.

Example:

Let us take the previous example, just replacing “in” operator with the “not in”.

#not in operator working

list1= [1,2,3,4,5]
string1= "My name is AskPython"
tuple1=(11,22,33,44)

print(5 not in list1) #False
print("is" not in string1) #False
print(88 not in tuple1) #True

Output:

Not In Output
not in Output

As expected, the resultant output is the exact opposite of what we got earlier using the “in” operator.

Using the “in” and “not in” Operators in Python Dictionaries

Previously we discussed the working of the “in” and “not in” operators on different types of sequences. But dictionaries are not a sequence, they are not organized in the order of lists and tuples. Instead, dictionaries are indexed on the basis of keys., i.e., dictionaries use key-value pairs to store and fetch data. 

So do the above operators work on dictionaries? And if they do, how do they evaluate the condition?

Let us try to understand with an example.

Example:

#in and not in operator working on Dictionary

dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}

print("one" in dict1)
print("one" not in dict1)

print(3 in dict1)
print(3 not in dict1)

print(5 in dict1)
print(5 not in dict1)

Output:

Using In And Not In On Dictionary
Using in And not in on Dictionary

Explanation:

  • Here we have initialised a dictionary dict1 with a certain set of keys and corresponding values.
  • In the output, “one” in dict1 evaluates into a False. Whereas, 3 in dict1 gives us True.
  • Meaning that the “in” operator looks for the element among the dictionary keys, not the values. Hence, similarly, the last statement 5 in dict1 also results in a False as it is not a key in the dictionary.

Here the “not in” operator also evaluates in the same way.

Conclusion

Let’s summarize what we’ve learned. In the Python programming language, there are two membership operators “in” and “not in” that can be used when you want to check whether a value or item is present in an iterator. “in” returns True if it is present and if it is not present it returns False, while “not in” is just the opposite, it returns False if it is present, and returns True if not present in a specific sequence. Hope you have successfully learned about “in” and “not in” operators through this tutorial.

Reference