remove( ) method :
remove( ) deletes the given element.
If the item does not exist, it will throw error
Python
fruits = {"apple", "banana", "mango"} fruits.remove("banana") print(fruits) #output {'apple', 'mango'} discard( ) method :
Removes the item only if it exists.
If the item does not exist, no error is occured.
Python
numbers = {1, 2, 3} numbers.discard(2) print(numbers) #output {1, 3} pop( ) method :
Since sets are unordered , pop( ) removes any arbitary element.
Python
colors = {"red", "blue", "green"} item = colors.pop() print(colors) print("Removed:", item) #output Removed: green #Output may vary clear( ) method :
Deletes all the elements , but keeps the set object.
Python
data = {10, 20, 30} data.clear() print(data) #output set()del :
Deletes the entire set object.
Python
numbers = {1, 2, 3} del numbers print(numbers) #output Name Error