How to Remove an Element From a Set in Python

by | Programming, Python, Tips

This tutorial will go through removing elements from a Python set using the remove() method.


Removing an Element from a Python Set using remove()

Syntax of Python Set remove()

The syntax of the remove() method is

set.remove(element)

The remove() method removes elements from the set in place; it does not return a new set.

Example#1: Remove an Element from a Set

We can use the remove() method to remove a given element from a set.

Let’s look at an example of a set of countries.

We will remove another even number to the set:

# Countries set

countries = {"France", "Belgium", "Tanzania", "Chile"}

# Remove "Belgium" from countries

countries.remove("Belgium")

#Print updated countries set

print(f'Set with Belgium removed: {countries}')

Let’s run the code to see the result:

Set with Belgium removed: {'Chile', 'Tanzania', 'France'}

Example 2: Remove an Element that does not Exist in a Set using remove()

If we try to remove an element that does not exist in the set, the Python interpreter will raise a KeyError. Let’s look at an example:

# Chemical elements set

elements = {"hydrogen", "oxygen", "nitrogen", "carbon"}

# Attempt to Remove Xenon

elements.remove("xenon")

print(elements)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
      5 # Attempt to Remove Xenon
      6 
----≻ 7 elements.remove("xenon")
      8 
      9 print(elements)

KeyError: 'xenon'

We can use the set discard() method to avoid this error.

Removing an Element from a Python Set using discard()

Syntax of Python Set discard()

The syntax of the discard() method is:

set.discard(element)

The discard() method removes elements from the set in place if the element is present. The discard() method does not return a new set; it returns None. If the element is not present, the set will remain unchanged.

Example 3: Remove an element from a Python Set using discard()

The discard() method removes the specified element from the set. If the element is not present in the set, the set will remain unchanged, and the interpreter will not raise the KeyError.

Let’s look at an example of the discard() method to remove elements from a set:

# prime numbers

prime_numbers = {1, 3, 5, 7, 11}

prime_numbers.discard(3)

print(f'Prime numbers = {prime_numbers}')

prime_numbers.discard(17)

print(f'Prime numbers = {prime_numbers}')

Let’s run the code to see the result:

Prime numbers = {1, 5, 7, 11}
Prime numbers = {1, 5, 7, 11}

The number 17 was not present in the set, so the second call of the discard() method leaves the set unchanged.

Summary

Congratulations on reading to the end of this tutorial. We have gone through how to remove elements from a set using remove() and discard().

For further reading on set methods, go to the article: How to Add to a Set in Python.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee