• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if Set Contains Element in Python

Check if Set Contains Element in Python

February 25, 2022 Leave a Comment

To check if a set contains a specific element in Python, you can use the Python in operator.

set_of_numbers = {0,1,2,3,4}

if 3 in set_of_numbers:
    print("3 is in the set of numbers!")
else:
    print("3 is not in the set of numbers!")

#Output:
3 is in the set of numbers

If you want to check if an element is not in a set, you can use the Python not in operator.

set_of_numbers = {0,1,2,3,4}

if 5 not in set_of_numbers:
    print("5 is not in the set of numbers!")
else:
    print("5 is in the set of numbers!")

#Output:
5 is not in the set of numbers

In Python, sets are unordered collections of items. When working with sets, it can be useful to know if certain elements are contained in a set.

We can easily check if a set contains an element in Python.

To check if a set contains an element, we can use the Python in operator. The in operator checks if an element is in a set and returns a boolean value.

Below is a simple example of how to use the in operator in Python to check if an element is in a set.

set_of_numbers = {0,1,2,3,4}

print(3 in set_of_numbers)
print(5 in set_of_numbers)

#Output:
True
False

How to Check if a Set Contains an Element with not in in Python

We can also go the other way and check if a set doesn’t contain an element. To check if a set doesn’t contain an element in Python, we use the Python not in operator.

Below are a few examples of how to use not in to check if a set doesn’t contain a particular element in Python.

set_of_numbers = {0,1,2,3,4}

print(3 not in set_of_numbers)
print(5 not in set_of_numbers)

#Output:
False
True

Hopefully this article has been useful for you to learn how to check if a set contains an element in Python.

Other Articles You'll Also Like:

  • 1.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 2.  for char in string – How to Loop Over Characters of String in Python
  • 3.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 4.  Flatten List of Tuples in Python
  • 5.  Using Python to Split String by Tab
  • 6.  Create Symbolic Link with Python os.symlink() Function
  • 7.  How to Read Pickle File from AWS S3 Bucket Using Python
  • 8.  Python Subtract Days from Date Using datetime timedelta() Function
  • 9.  Append Multiple Elements to List Using Python
  • 10.  How to Check if a Letter is in a String Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy