Table of Contents
ToggleRemoving set items implies deleting elements from a set. In Python, sets are mutable, unordered collections of unique elements, and there are several methods available to remove items from a set based on different criteria.
We can remove set items in Python using various methods such as remove(), discard(), pop(), clear(), and set comprehension. Each method provides different ways to eliminate elements from a set based on specific criteria or conditions.
The remove() method in Python is used to remove the first occurrence of a specified item from a set.
We can remove set items using the remove() method by specifying the element we want to remove from the set. If the element is present in the set, it will be removed. However, if the element is not found, the remove() method will raise a KeyError exception.
my_set = {"Rohan", "Physics", 21, 69.75}
print("Original set: ", my_set)
my_set.remove("Physics")
print("Set after removing: ", my_set)
It will produce the following output:
Original set: {21, 69.75, 'Rohan', 'Physics'}
Set after removing: {21, 69.75, 'Rohan'}
my_set = {"Rohan", "Physics", 21, 69.75}
print("Original set: ", my_set)
my_set.remove("PHP")
print("Set after removing: ", my_set)
We get the following error:
KeyError: 'PHP'
The discard() method in the set class is similar to remove() but does not raise an error if the element is not present.
my_set = {"Rohan", "Physics", 21, 69.75}
print("Original set: ", my_set)
# removing an existing element
my_set.discard("Physics")
print("Set after removing Physics: ", my_set)
# removing non-existing element
my_set.discard("PHP")
print("Set after removing non-existent element PHP: ", my_set)
Output:
Original set: {21, 'Rohan', 69.75, 'Physics'}
Set after removing Physics: {21, 'Rohan', 69.75}
Set after removing non-existent element PHP: {21, 'Rohan', 69.75}
The pop() method removes and returns an arbitrary element from the set. If the set is empty, it raises a KeyError exception.
my_set = {1, 2, 3, 4, 5}
removed_element = my_set.pop()
print("Removed Element:", removed_element)
print("Updated Set:", my_set)
Output:
Removed Element: 1
Updated Set: {2, 3, 4, 5}
The clear() method removes all elements from a set, making it empty.
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print("Updated Set:", my_set)
Output:
Updated Set: set()
The difference_update() method removes elements that exist in both sets.
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1.difference_update(s2)
print("s1 after difference_update:", s1)
Output:
s1 after difference_update: {1, 2, 3}
Use the symmetric difference (^) or symmetric_difference() method.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result_set = set1 ^ set2
print("Resulting Set:", result_set)
Output:
Resulting Set: {1, 2, 5, 6}
The intersection_update() method keeps only common elements.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.intersection_update(set2)
print("Set 1 after keeping only common items:", set1)
Output:
Set 1 after keeping only common items: {3, 4}
The symmetric_difference_update() method modifies a set by removing common elements and keeping unique ones.
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s1.symmetric_difference_update(s2)
print("s1 after running symmetric difference ", s1)
Output:
s1 after running symmetric difference {1, 2, 3, 6, 7, 8}
The symmetric_difference() method returns a new set containing unique elements from both sets.
s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
s3 = s1.symmetric_difference(s2)
print("s1 = s1^s2 ", s3)
Output:
s1 = s1^s2 {1, 2, 3, 6, 7, 8}
