• 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
  • VBA
  • 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.  Python Round to Nearest 10 With round() Function
  • 2.  Find Maximum of Three Numbers in Python
  • 3.  How to Check if Set is Empty in Python
  • 4.  Python Random Uniform – Generate Random Numbers Uniformly in Range
  • 5.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 6.  Adjusting Python Turtle Screen Size with screensize() Function
  • 7.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 8.  Rename Key in Dictionary in Python
  • 9.  Sign Function in Python – Get Sign of Number
  • 10.  Python Add Months to Datetime Variable Using relativedelta() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy