To remove a specific item from a set in Python, you can use the remove method to delete the item from the set.
In this example,
number_set
using the curly braces syntax, which contains integer values 1
, 2
, 3
, 4
, and 5
.remove
method of number_set
to remove the item 3
from the set.number_set
to see the removed item.number_set = {1, 2, 3, 4, 5}
number_set.remove(3)
print(number_set)
{1, 2, 4, 5}
In this example,
string_set
using the curly braces syntax, which contains string values 'apple'
, 'banana'
, 'cherry'
, 'date'
, and 'grape'
.remove
method of string_set
to remove the item 'cherry'
from the set.string_set
to see the removed item.string_set = {'apple', 'banana', 'cherry', 'date', 'grape'}
string_set.remove('cherry')
print(string_set)
{'apple', 'banana', 'date', 'grape'}
In this tutorial, we learned How to Remove Specific Item from a Set in Python language with well detailed examples.