Python Tutorials · Python Sets

Remove Set Items

Learn all about Remove Set Items in this comprehensive tutorial.

5 min read beginner
  • To remove an item in a set, use the remove(), or the discard() method.

Remove Item

To remove an item in a set, use the remove(), or the discard() method.

python
Note: Note: If the item to remove does not exist, remove() will raise an error.
python
Note: Note: If the item to remove does not exist, discard() will NOT raise an error.

You can also use the pop() method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed.

The return value of the pop() method is the removed item.

python
Note: Note: Sets are unordered, so when using the pop() method, you do not know which item that gets removed.
python
python

Module quiz

2 questions
1

Which of the following is true about Remove Set Items?

2

What is the most common pitfall when working with Remove Set Items?

Answer all questions to submit.