DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 49 of 77
←PreviousPrevNextNext→

Remove Set Items

Removing Items from Sets

Sets provide several ways to remove elements, each with different behavior:

- **remove()**: Deletes a specific element. Raises `KeyError` if not present.

- **discard()**: Deletes a specific element. Safe to use—does nothing if the element is absent.

- **pop()**: Removes and returns an arbitrary element (sets are unordered, so result is unpredictable).

- **clear()**: Removes all elements, leaving an empty set.

Example
# Removing items from a set
fruits = {"apple", "banana", "cherry", "orange"}

# Using remove()
fruits.remove("banana")
print(fruits)

# Using discard()
fruits.discard("mango")  # No error if element not present
print(fruits)

# Using pop()
removed = fruits.pop()
print(f"Removed: {removed}")
print(fruits)

# Using clear()
fruits.clear()
print(fruits)
Output
{'cherry', 'apple', 'orange'}
{'cherry', 'apple', 'orange'}
Removed: cherry
{'apple', 'orange'}
set()

Difference Between remove() and discard()

`remove()` raises an error if the element is missing, making it suitable when the presence of the element is required.

`discard()` ignores missing elements, which makes it safer for optional removals.

Example
# remove() vs discard()
letters = {"a", "b", "c"}

# letters.remove("z")  # KeyError if uncommented
letters.discard("z")  # Safe, no error
print(letters)
Output
{'a', 'c', 'b'}

Using pop() Carefully

`pop()` removes and returns an arbitrary element since sets are unordered.

It is useful for consuming elements until the set is empty, but not for predictable removals.

Example
# Using pop() in a loop
numbers = {1, 2, 3, 4}

while numbers:
    removed = numbers.pop()
    print(f"Popped: {removed}, Remaining: {numbers}")
Output
Popped: 1, Remaining: {2, 3, 4}
Popped: 2, Remaining: {3, 4}
Popped: 3, Remaining: {4}
Popped: 4, Remaining: set()

Clearing vs Deleting a Set

`clear()` empties a set but keeps the object alive for reuse.

`del` completely removes the set object from memory, making its name undefined.

Example
# clear() vs del
fruits = {"apple", "banana"}
fruits.clear()
print(fruits)  # set()

numbers = {1, 2, 3}
del numbers
# print(numbers)  # NameError: name 'numbers' is not defined
Output
set()
(NameError when trying to access numbers after deletion)

Best Practices

  • Use `discard()` when the element may or may not exist.
  • Use `remove()` when you want errors for missing elements.
  • Avoid `pop()` for ordered removals—sets don’t preserve order.
  • Use `clear()` to empty a set, or `del` to remove the set object completely.
Test your knowledge: Remove Set Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 49 of 77
←PreviousPrevNextNext→