In this article, we are going to learn Python Membership and Identity Operators.
Membership Operators
Python offers two membership operators to check or validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples.
in operator: The ‘in’ operator is used to check if a character/ substring/ element exists in a sequence or not. Evaluate to True if it finds the specified element in a sequence otherwise False. For example,
'G' in 'GeeksforGeeks' # Checking 'G' in String
True
'g' in 'GeeksforGeeks' #Checking 'g' in string since Python is case-sensitive,returns False
False
'Geeks' in ['Geeks', 'For','Geeks'] #Checking 'Geeks' in list of strings
True
10 in [10000,1000,100,10] #Checking 10 in list of integers
True
dict1={1:'Geeks',2:'For',3:'Geeks'} # Checking 3 in keys of dictionary
3 in dict1
True
Python3
list1 = [ 1 , 2 , 3 , 4 , 5 ]
list2 = [ 6 , 7 , 8 , 9 ]
for item in list1:
if item in list2:
print ( "overlapping" )
else :
print ( "not overlapping" )
|
Output
not overlapping
not overlapping
not overlapping
not overlapping
not overlapping
The same example without using in operator:
Python3
def overlapping(list1, list2):
c = 0
d = 0
for i in list1:
c + = 1
for i in list2:
d + = 1
for i in range ( 0 , c):
for j in range ( 0 , d):
if (list1[i] = = list2[j]):
return 1
return 0
list1 = [ 1 , 2 , 3 , 4 , 5 ]
list2 = [ 6 , 7 , 8 , 9 ]
if (overlapping(list1, list2)):
print ( "overlapping" )
else :
print ( "not overlapping" )
|
The execution speed of the ‘in’ operator depends on the target object’s type.
The average time complexity of the ‘in’ operator for lists is O(n). It becomes slower as the number of elements increases.
The average time complexity of the ‘in’ operator for sets is O(1). It does not depend on the number of elements.
For dictionaries, the keys in the dictionary are unique values like set. So the execution is same as the set. Whereas the dictionary values can be repeated as in a list. So the execution of ‘in’ for values() is same as lists.
‘not in’ operator- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
Python3
x = 24
y = 20
list = [ 10 , 20 , 30 , 40 , 50 ]
if (x not in list ):
print ( "x is NOT present in given list" )
else :
print ( "x is present in given list" )
if (y in list ):
print ( "y is present in given list" )
else :
print ( "y is NOT present in given list" )
|
Output
x is NOT present in given list
y is present in given list
Identity operators
Identity operators are used to compare the objects if both the objects are actually of the same data type and share the same memory location.
There are different identity operators such as
‘is’ operator – Evaluates to True if the variables on either side of the operator point to the same object and false otherwise.
Python3
x = 5
y = 5
print (x is y)
id (x)
id (y)
|
Here in the given example, both the variables x and y have value 5 assigned to it and both share the same memory location, which is why return True.
‘is not’ operator: Evaluates True if both variables are not the same object.
Python3
x = [ "Geeks" , "for" , "Geeks" ]
y = [ "Geeks" , "for" , "Geeks" ]
z = x
print (x is not z)
print (x is not y)
print (x ! = y)
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Apr, 2023
Like Article
Save Article