How to Check If a Set Contains an Element in Python

Python set contains

There are the following methods to check if a set contains an element in Python.

  1. Method 1: Using the “in” operator
  2. Method 2: Using the “not in” operator
  3. Method 3: Using the combination of Counter() and dict.keys() methods

Method 1: Using the “in” operator

To check if the Set contains an element in Python, use the in keyword, which returns True if the specified Set contains an element and False otherwise.

The “in operator” checks if the item is in a sequence like a list, range, string, or Set.

The “in keyword” is also used to iterate through a sequence in a for loop. 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.

first_set = {11, 19, 21}

el_in_set = 19 in first_set

print(el_in_set)

Output

True

It returns True because the 19 is included in the Set.

Check for element 46 in the existing Set and see the output.

first_set = {11, 19, 21}

el_in_set = 46 in first_set

print(el_in_set)

Output

False

We got False because the Set does not contain the “46” element.

Method 2: Using the “not in” operator

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

first_set = {11, 19, 21}

el_in_set = 46 not in first_set

print(el_in_set)

Output

True

It returns True because the Set does not contain 46. That is why not in operator returns True because it is not in the Set. Let’s take an example where the element exists in the Set and see the output.

first_set = {11, 19, 21}

el_in_set = 11 not in first_set

print(el_in_set)

Output

False

And it returns False, which is correct because 11 exists in the Set.

Method 3: Using the Counter() Function

The Counter is a dict subclass and collections module method used to count the occurrences of elements in an iterable. It can help count elements, notably when you have many elements that are either repeated or unique.

Using the Counter() with dict.keys() method, we can check if the Set contains a specific element.

from collections import Counter

set = {21, 19, 18, 11, 46}

element_dict = Counter(set)

print(21 in element_dict.keys())
print(20 in element_dict.keys())
print(19 in element_dict.keys())

Output

True
False
True

First, we declared a set with five elements.

Using the Counter() function, we converted a set to a dictionary and checked if the dictionary’s keys existed using the dict.keys() method.

This way, we can check whether the set element exists without using any built-in operator.

The Time Complexity is O(N), and the Auxiliary Space complexity is O(N).

Conclusion

The best and most efficient way to check if a set contains an element is to use the “in operator” in Python.

The Counter() and dict.keys() approach can be helpful in specific scenarios.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.