6.1 remove()
and discard()
Functions
Sets in Python give you a couple of ways to remove elements. Below
we will look at remove()
, discard()
,
pop()
, clear()
, and using the
del
operator to delete a set entirely. Each method has its own quirks
and use cases.
The two most common ways to remove elements are the
remove()
and discard()
methods.
The remove()
Method
The remove()
method deletes the specified element from the set. If
the element doesn't exist in the set, you'll get a KeyError
.
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5}
# If the element isn't in the set, an error occurs
my_set.remove(6) # KeyError: 6
The discard()
Method
The discard()
method also removes the specified element from the set,
but unlike remove()
, it doesn't throw an error if the element is
not in the set.
my_set = {1, 2, 3, 4, 5}
my_set.discard(3)
print(my_set) # Output: {1, 2, 4, 5}
# If the element isn't in the set, no error occurs
my_set.discard(6)
print(my_set) # Output: {1, 2, 4, 5}
6.2 pop()
and clear()
Functions
The pop()
and clear()
functions are also commonly used.
The pop()
Method
The pop()
method removes and returns a random element from
the set. If the set is empty, a KeyError
pops up.
my_set = {1, 2, 3, 4, 5}
removed_element = my_set.pop()
print(removed_element) # Output: One of the elements of the set, e.g., 1
print(my_set) # Output: The remaining elements of the set, e.g., {2, 3, 4, 5}
# If the set is empty, you get an error
empty_set = set()
empty_set.pop() # KeyError: 'pop from an empty set'
Example: Removing all elements from a set using
pop()
The pop()
method can be used in a loop to remove all elements
from the set one by one until it's empty.
my_set = {1, 2, 3, 4, 5}
print("Original set:", my_set)
while my_set:
removed_element = my_set.pop()
print(f"Removed element: {removed_element}, Remaining elements: {my_set}")
print("Set is empty:", my_set)
Output
Original set: {1, 2, 3, 4, 5}
Removed element: 1, Remaining elements: {2, 3, 4, 5}
Removed element: 2, Remaining elements: {3, 4, 5}
Removed element: 3, Remaining elements: {4, 5}
Removed element: 4, Remaining elements: {5}
Removed element: 5, Remaining elements: set()
Set is empty: set()
The clear()
Method
The clear()
method removes all elements from the set, making it
empty.
Example
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print(my_set) # Output: set()
6.3 Using the del
Operator
The del
Operator in Python is used to
delete objects. When dealing with sets, the del
operator can be
used to delete the entire set, freeing up memory.
Below are a couple of examples of using the del
operator with sets.
Deleting a Set
When the del
operator is used to delete a set, that
set no longer exists, and all its elements are removed from memory.
my_set = {1, 2, 3, 4, 5}
print("Original set:", my_set)
# Deleting the set
del my_set
# Trying to access the deleted set will cause an error
# print(my_set) # NameError: name 'my_set' is not defined
In this example, the set my_set
is deleted using the
del
operator. After that, any attempt to access my_set
will raise a NameError
because the object no longer exists.
GO TO FULL VERSION