In this tutorial, we will look at how to check if a set contains an element or not in Python with the help of some examples.
How to check if an element is in a set?

You can use the membership operator in
to check if a set contains an element or not in Python. The following is the syntax:
# check if element e is in set a e in a
It returns a boolean value. True
if the element is present in the set and False
if it isn’t.
Let’s look at an example –
# create a set a = {1, 2, 3} # check if 3 is in a print(3 in a)
Output:
True
Here we check whether the element 3
is in the set a or not. We get True as the output because the set a
, here, contains the element 3
.
Let’s look at another example.
# create a set a = {1, 2, 3} # check if 4 is in a print(4 in a)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
False
Here we get False
as the output because the set a
does not contain the element 4
.
Alternatively, you can also use the set issubset() function to check whether a set contains an element or not. The idea is to check whether the set contains a subset with the element we want to check for. Here’s an example.
# create a set a = {1, 2, 3} # check if 3 is in a print({3}.issubset(a)) # check if 4 is in a print({4}.issubset(a))
Output:
True False
We get the correct result.
Note that you should prefer the membership operator in
to using the issubset() function for checking whether a set contains an element or not as it is simple and intuitive.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.