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.

Categorized in:

Python,

Last Update: March 15, 2024