The Wayback Machine - https://web.archive.org/web/20240108220843/https://www.geeksforgeeks.org/how-to-check-if-a-set-contains-an-element-in-python/
Open In App
Related Articles

How to check if a set contains an element in Python?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will discuss how to check if a set contains an element in python.

Method: 1 Using in operator

This is an membership operator used to check whether the given value is present in set or not. It will return True if the given element is present in set , otherwise False.

Syntax:

element in set

where

  • set is an input set
  • element is the value to be checked

Example: Check if an element is present in a set

Python3




# import random module
import random
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
 
# check 7058
print(7058 in data)
 
# check 7059
print(7059 in data)
 
# check 7071
print(7071 in data)


Output:

True
True
False

Method 2: Using not in operator

This is an membership operator used to check whether the given value is present in set or not. It will return True if the given element is not present in set, otherwise False

Syntax:

element not in set

where,

  • set is an input set
  • element is the value to be checked

Example: Check if an element is present in a set

Python3




# import random module
import random
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
 
# check 7058
print(7058 not in data)
 
# check 7059
print(7059 not in data)
 
# check 7071
print(7071 not in data)


Output:

False
False
True

Method 3: Using Counter() Function

Python3




from collections import Counter
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
freq = Counter(data)
# check 7058
print(7058 in freq.keys())
 
# check 7059
print(7059 in freq.keys())
 
# check 7071
print(7071 in freq.keys())


Output

True
True
False

Time Complexity:O(N)

Auxiliary Space: O(N)

Method #4 : Using operator.countOf() method.

Python3




import operator as op
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
# check 7058
print(op.countOf(data, 7058) > 0)
 
# check 7059
print(op.countOf(data, 7059) > 0)
 
# check 7071
print(op.countOf(data, 7071) > 0)


Output

True
True
False

Time Complexity: O(n)

Auxiliary Space: O(1)


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials