• 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 / Remove Element from Set in Python

Remove Element from Set in Python

February 20, 2022 Leave a Comment

In Python, we can easily remove an element from a set in a number of ways. The easiest way to remove an element from a set is with the remove() function.

set_with_elements = {"whale","dog","cat"}

set_with_elements.remove("dog")

print(set_with_elements)

#Output:
{"whale","cat"}

You can also use the discard() function to remove items from a set in Python.

set_with_elements = {"whale","dog","cat"}

set_with_elements.discard("dog")

print(set_with_elements)

#Output:
{"whale","cat"}

If you want to get remove the last element from a set, you can use the pop() function.

set_with_elements = {"whale","dog","cat"}

set_with_elements.pop()

print(set_with_elements)

#Output:
{"whale","dog"}

Finally, if you want to remove all elements from a set, you can use the clear() function.

set_with_elements = {"whale","dog","cat"}

set_with_elements.clear()

print(set_with_elements)

#Output:
set()

In Python, sets are a collection of elements which are unordered and mutable. When working with sets, it can be useful to be able to easily add or remove items.

The easiest way to remove an element from a set is with the remove() function. To remove an element from a set, pass the element value to remove().

Below is an example of how to remove an element from a set using the remove() function in Python.

set_with_elements = {"whale","dog","cat"}

set_with_elements.remove("dog")

print(set_with_elements)

#Output:
{"whale","cat"}

Note, if the element that you are trying to remove does not exist, you will get a KeyError.

How to Remove Element from Set Using discard() in Python

Another way to remove an element from a set is to use the Python set discard() function. To remove an item from a set using discard(), simply pass the element value to the function.

The difference between discard() and remove() is that if try to remove an element that doesn’t exist with discard(), you won’t receive an error.

Below is an example of how to remove an element from a set using the discard() function in Python.

set_with_elements = {"whale","dog","cat"}

set_with_elements.discard("dog")

print(set_with_elements)

#Output:
{"whale","cat"}

How to Remove Last Item from a Set Using pop() in Python

Just like with other collections in Python, we can remove the last item from a set using the pop() function. This is the same pop() function that we can use for removing the last item from a list.

Below is an example of how to remove the last item from a set using the pop() function in Python.

set_with_elements = {"whale","dog","cat"}

set_with_elements.pop()

print(set_with_elements)

#Output:
{"whale","dog"}

How to Remove All Elements from a Set Using clear() in Python

Finally, if you want to remove all elements from a set, you can use the clear() function. The clear() function removes all items from a set and leaves you with an empty set.

Below is an example of how to remove all items from a set using the clear() function in Python.

set_with_elements = {"whale","dog","cat"}

set_with_elements.clear()

print(set_with_elements)

#Output:
set()

Hopefully this article has been useful for you to learn how to remove an element from a set in Python.

Other Articles You'll Also Like:

  • 1.  Writing Multiple Lines Lambda Expression in Python
  • 2.  How to Group By Columns and Find Variance in pandas
  • 3.  Symmetric Difference of Two Sets in Python
  • 4.  Solve Python Object Does Not Support Indexing Error
  • 5.  Replace Values in Dictionary in Python
  • 6.  Using Python to Convert Timestamp to Date
  • 7.  Python Random Boolean – How to Generate Random Boolean Values
  • 8.  Check if String Does Not Contain Substring in Python
  • 9.  List of Turtle Shapes in Python
  • 10.  Count Number of Keys in Dictionary in 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