How to Check if a Set Contains an Element in Python?

How to Check if a Set Contains an Element in Python?

This article will explore the method for determining the presence of an element within a set using Python. Here, ‘set’ represents the input set, and ‘element’ denotes the value being examined.

Method: 1 Using an Operator

This operator checks if a value is in a set. If the element is found, it returns True; otherwise, it returns False.

Syntax


element in set

Example


# Define a set
my_set = {1, 2, 3, 4, 5}
# check 7058
print(7 in my_set)
# check 7059
print(5 in my_set)
# check 7071
print(0 in my_set)

Output

False
True
False

Method 2: Using not in Operator

This operator checks if a value exists in a set. It returns True if the value is present, and False if it’s not. Here, ‘set’ represents the set we’re checking, and ‘element’ is the value we’re verifying for presence.

Syntax


element not in set

Example


# Define a set
my_set = {1, 2, 3, 4, 5}
# check 7058
print(7 not in my_set)
# check 7059
print(5 not in my_set)
# check 7071
print(0 not in my_set)

Output

True
False
True

Save $100 in the next
5:00 minutes?

Register Here

Method 3: Using Counter() Function

The Counter() function from the Python collections module is typically used to count the occurrences of elements in a collection, such as a list or a string. However, it can also be used to check if an element is present in a set indirectly by converting the set into a Counter object and then checking the count of the element. Here’s an

Example


from collections import Counter
# Define a set
input_set = {1, 2, 3, 4, 5}
# Element to be checked
element = 3
# Convert the set into a Counter object
set_counter = Counter(input_set)
# Check if element is present by checking its count
is_present = set_counter[element] > 0
# Print the result
print("Is", element, "present in the set?", is_present)

Output

Is 3 present in the set? True

Save $100 in the next
5:00 minutes?

Register Here

Method 4: Using operator.countOf() method

The operator.countOf() method is used to count the occurrences of a specific element in a sequence, such as a list or a string. We can indirectly use this method to check if an element is present in a set by converting the set into a list and then applying the countOf() method.

Here’s an example


import operator
# Define a set
input_set = {1, 2, 3, 4, 5}
# Element to be checked
element = 3
# Convert the set into a list
set_list = list(input_set)
# Check if element is present using countOf() method
is_present = operator.countOf(set_list, element) > 0
# Print the result
print("Is", element, "present in the set?", is_present)

Output

Is 3 present in the set? True

Conclusion

Each method provides a way to determine the presence of an element in a set, offering flexibility based on the specific requirements and preferences of the programmer. Depending on the context and performance considerations, one method may be preferred over another.

Save $100 in the next
5:00 minutes?

Register Here