Membership Test Operators in Python

In the previous article we have gone through the bitwise operators available in Python, to further our knowledge of Operators in Python we will look at how to effectively utilize Membership Operators. Sometimes we are required to validate whether a certain data collection possesses specified items,  there are inbuilt operators called membership operators in Python , designed to check for the presence of an element in complex data types such as strings, lists, tuples, or dictionaries. Other languages usually do not have a built-in operator with this kind of utility.

Utilizing the Membership Test Operators in Python

There are only two membership test operators in Python and they are designed to check if an element exists in a string (str), list (list), dictionary (dict), or tuple. They return a Boolean result of True or False depending upon whether the element exists in the collection/ iterable or not.

  • in – returns True if the element is present in the sequence;
  • not in – Returns True if the element is not present in the sequence.
OperatorDescriptionExamples of
inReturns true if the element is present in the sequence, otherwise returns false.“cad” in “cadillac” True.1 in [2,3,1,6] will return True. “hi” in {“hi”:2,”bye”:1} True.2 in {“hi”: 2, “bye”: 1} will return False (dictionaries are checked for presence in keys, not in values).
not inReturns true if there is no element in the sequence.The results are the opposite of those of the in operator.

Usage of ‘in’ operator

Checks if a value is a member of a sequence. This operator can be used with loops and conditions and even just to ensure that a specific value is present when taking user input. In this example, you can see that the element of fox is not in the pet list. But cat is, so it returns True. Also, the string me is a substring  of the string disappointment. Therefore, she will return True.

pets=['dog', 'cat', 'ferret']
print('fox' in pets)
print('cat' in pets)
x = ["apple", "banana"]
print("banana" in x)

dictnames = {"name": "Pamela", "id": 1}
print("name" in dictnames)  
print("Pamela" in dictnames)  

Output:
False
True
True
True
False
print('me' in 'appointment')
Output:
True

Usage of ‘not in’ operator

The not in operator is the opposite of the in operator. While it checks the presence of a particular element within a sequence, it returns the opposite value. This operator checks to see if the value is not a member of a sequence.

print('pot' not in 'disappointment')
Output:
True
l=[1,2,3]
print(1 in l)
print(1 not in l)
primes=(2,3,5,7,11)
6 not in primes
3 not in primes
Output:
True
False
True
False
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
   print ("Line 1 - a is available in the given list")
else:
   print ("Line 1 - a is not available in the given list")

if ( b not in list ):
   print ("Line 2 - b is not available in the given list")
else:
   print ("Line 2 - b is available in the given list")

a = 2
if ( a in list ):
   print ("Line 3 - a is available in the given list")
else:
   print ("Line 3 - a is not available in the given list")
Output:
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list

Hence we have explored the membership operators in Python, along with demonstrations of it’s utility through examples. The in and not in operators are very useful for checking for certain elements rapidly without having to utilize equality checks. In the next article we will explore Arithmetic Progression in Python.

Leave a Comment