Python Membership Operator

The membership operators in Python will allow us to know if an item is available in the given container-type object. Let us look at this article to learn more about Python Membership Operator

What is a Python Membership Operator?

Python will provide two membership operators for checking the membership value. It will check for membership in a string, list, or tuples sequence.

What is In operator?

The in-operator will monitor whether a character or substring will be present in the sequence. So, this operator will be considered true only if it can find the specified element, or else it will be false.

Example

'S' in 'Skillvertex'   # Checking 'S' in String
True
's' in 'Skillvertex'   #Checking 's' in string since Python is case-sensitive,returns False
False
'Skill' in ['Skill','Vertex']   #Checking 'Skill' in list of strings
True
10 in [10000,1000,100,10]        #Checking 10 in list of integers
True
dict1={1:'Skill,2:'For',3:'Skill'}     # Checking 3 in keys of dictionary
3 in dict1
True

Python Program to find the common member is provided below

# Membership operator example
fruits = ['apple', 'banana', 'orange', 'grape']

# Using 'in' operator
search_fruit = 'banana'
if search_fruit in fruits:
    print(f"{search_fruit} is present in the list.")
else:
    print(f"{search_fruit} is not present in the list.")

# Using 'not in' operator
search_fruit = 'kiwi'
if search_fruit not in fruits:
    print(f"{search_fruit} is not present in the list.")
else:
    print(f"{search_fruit} is present in the list.")

Output

banana is present in the list.
kiwi is not present in the list.

Now, the Python Program below will use the same example without an operator.

# Membership operator example without using operators
fruits = ['apple', 'banana', 'orange', 'grape']

# Without using 'in' operator
search_fruit = 'banana'
found = False
for fruit in fruits:
    if fruit == search_fruit:
        found = True
        break
if found:
    print(f"{search_fruit} is present in the list.")
else:
    print(f"{search_fruit} is not present in the list.")

# Without using 'not in' operator
search_fruit = 'kiwi'
not_found = True
for fruit in fruits:
    if fruit == search_fruit:
    not_found = False
        break
if not_found:
    print(f"{search_fruit} is not present in the list.")
else:
    print(f"{search_fruit} is present in the list.")

Output

banana is present in the list.
kiwi is not present in the list.

The execution speed of the in-operator will mainly depend on the target object type. The average time complexity of the in operator for the list is 0(n). Hence, it will get slow due to the number of elements increasing.

Moreover, the average time complexity of the in-operator for sets is 0(1). Hence, it won’t rely on the number of elements.

Whereas in dictionaries, the keys in the dictionary are unique values such as set. It is possible to repeat the value as similar to the list. So, the ‘in’ for values () will be executed as similar to the lists.

What is NOT in the operator?

This NOT in operator can run the program to get the true value if it can’t find the variable in the specified sequence or else, it will be declared as false.

Take a look at the Python Program to illustrate the not-in operator.

# Membership operator 'not in' example with output
fruits = ['apple', 'banana', 'orange', 'grape']

# Using 'not in' operator
search_fruit = 'kiwi'
if search_fruit not in fruits:
    print(f"{search_fruit} is not present in the list.")
else:
    print(f"{search_fruit} is present in the list.")

Output

kiwi is not present in the list.

This example mentions that the not-in operator will be used to evaluate the value such as kiwi is not present in the list of fruits.

What is an Identity Operator?

The identity operator functions to compare the objects as both the objects are the same data type and will share the same memory location.

What ‘is’ operator

This ‘is’ operator will be used to check if the value can come true, only if the variable on either side of the operator points to the same object or else it will be false.

# Using 'is' operator for membership-like testing
fruits = ['apple', 'banana', 'orange', 'grape']

# Object to test for membership
search_fruit = 'banana'

# Check using 'is' operator
is_member = any(fruit is search_fruit for fruit in fruits)

if is_member:
    print(f"{search_fruit} is in the list.")
else:
    print(f"{search_fruit} is not in the list.")

Output

banana is in the list.

What is ‘Is not’ operator

This ‘Is not’ operator will check if the values come true, only if both variables are not in the same object.

# Using 'is not' operator for membership-like testing
fruits = ['apple', 'banana', 'orange', 'grape']

# Object to test for non-membership
search_fruit = 'kiwi'

# Check using 'is not' operator
is_not_member = all(fruit is not search_fruit for fruit in fruits)

if is_not_member:
    print(f"{search_fruit} is not in the list.")
else:
    print(f"{search_fruit} is in the list.")

Output

kiwi is not in the list.

Conclusion

To conclude, in Python membership operators (in and not in) will allow us to check if something is part of a group, such as looking if a specific word is in a list.

If it’s on the list, it will give us True, otherwise False. On the other hand, not in does the opposite; it gives us True if the thing is not in the group, and False. This article has also discussed the different operators of the membership operator. Those operators include ‘In’ operator, ‘not in’ operator, and several other identity operators.

Python Membership Operator- FAQs

Q1. What is membership testing in Python?

Ans. Membership testing is used to check if the collection of list has a specific item such as list , set or dictionary.

Q2. How many operators are in Python?

Ans. There are seven different operators in Python. Those operators are arithmetic, assignment, comparison, logical, identity, membership, and boolean operators.

Q3. What is a tuple example?

Ans. The Tuplr is referred to as an ordered as the uniqueness in order will define the tuple. Examples of Tuple are a1 equals b1, a2 equals b2, a3 equals b3 

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment