In this tutorial, we will look at how to remove elements from a set in Python with the help of some examples.
How to remove an item from a set in Python?
The python set data structure comes with a number of built-in functions to remove items from a set. You can use the python set remove()
, discard()
, and pop()
functions to remove elements from a set. Note that there are slight differences between each of these functions which we will look at with the help of examples.
Examples
Let’s look at the usage and outcomes of each of the above-mentioned functions.
Using set remove()
to remove elements from a set
Pass the item you want to remove from the set as an argument to the set remove()
function. If the item is present in the set, it removes the item but does not return any value (returns None
).
# create a set pets = {'cat', 'dog', 'bunny'} # remove an item using remove() val = pets.remove('bunny') # print the returned value print(val) # print the set print(pets)
Output:
None {'cat', 'dog'}
You can see that “bunny” was removed from the set. Also, note that the function did not return any value.
Note that if the item is not present in the set, the set remove()
function gives a KeyError
.
# create a set pets = {'cat', 'dog', 'bunny'} # remove an item not present in the set using remove() pets.remove('fish')
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_2908/991581951.py in <module> 2 pets = {'cat', 'dog', 'bunny'} 3 # remove an item not present in the set using remove() ----> 4 pets.remove('fish') KeyError: 'fish'
We get a KeyError
here because the item “fish” is not present in the set.
Using set discard()
to remove elements from a set
The set discard()
function works similarly to the set remove()
function. Pass the item you want to remove from the set as an argument. It removes the item from the set but does not return any value.
# create a set pets = {'cat', 'dog', 'bunny'} # remove an item using discard() val = pets.discard('bunny') # print the returned value print(val) # print the set print(pets)
Output:
None {'cat', 'dog'}
We get the same result as we got from the reomve()
function but if the item is not present in the set, it does not raise any errors.
# create a set pets = {'cat', 'dog', 'bunny'} # remove an item not present in the set using discard() pets.discard('fish') # display the set print(pets)
Output:
{'bunny', 'cat', 'dog'}
You can see that we didn’t get any errors when trying to remove an item not present in the set.
Using set pop()
to remove remove elements from a set
The set pop()
function does not take any arguments and arbitrarily removes any element from the set. It also returns the element being removed.
# create a set pets = {'cat', 'dog', 'bunny'} # remove an item using pop() val = pets.pop() # print the returned value print(val) # print the set print(pets)
Output:
bunny {'cat', 'dog'}
The item “bunny” was removed from the set and was returned by the pop()
function.
Summary
You can use the python set remove()
, discard()
, pop()
functions to remove elements from a set. There are slight differences between them –
remove()
– Removes the item from the set if it’s present, if it’s not, it raises aKeyError
. It does not return any value (returnsNone
).discard()
– Removes the item from set if it’s preset but does not raise any errors if the item is not present. It does not return any value (returnsNone
).pop()
– Does not take any arguments and removes arbitrarily any element from the set. It returns the removed item.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.