Definition
When you want to delete a value, the remove() method checks for the value in the set. If it finds the value, it deletes it; otherwise, an error message appears.
Now you will clearly understand the method through the flowchart.
s = {10, 20, 30}
========================
CASE 1: Value Exists
========================
remove(20)
20
│
▼
{10, 20, 30}
│
▼
{10, 30}
========================
CASE 2: Value Not Exists
========================
remove(50)
50
│
▼
{10, 20, 30}
│
▼
ERROR (Value not found)
Syntax:
set_name.remove(element)
Example 1:
s = {10, 20, 30}
s.remove(20)
print(s)
Output:
Example 2:
s = {10, 20, 30}
s.remove(50) # Display the error message because it is not in the set.
print(s)
Output:
Exercise
Create a set s = {1, 2, 3} and remove 2 from it.
