Skip to Content

Remove Element from a Set in Python

In this tutorial, we will look at how to remove elements from a set in Python with the help of some examples.

Image showing an item getting removed from a set

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.

Let’s look at the usage and outcomes of each of the above-mentioned functions.

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:

---------------------------------------------------------------------------
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.

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.

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.

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 a KeyError. It does not return any value (returns None).
  • 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 (returns None).
  • 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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.