After writing a set, do you sometimes wish to remove certain elements from it? This is possible by using the python set remove function. The python set remove function removes the specified element from the set.
Syntax:
set.remove(element)
remove() Parameters
The remove() takes only one parameter as the input from the user. This is the element that must be removed from the set.
Return Value from remove()
The remove() in python removes the element from the set it does not return any value. If the element mentioned by the user does not exist in the set, then an exception is thrown.
Example 1: Remove an Element From The Set
# language set
language = {'English', 'French', 'German'}
# removing 'German' from language
language.remove('German')
# Updated language set
print('Updated language set:', language)
Output
Updated language set: {'English', 'French'}
Example 2: Deleting Element That Does Not Exist
# animal set
animal = {'cat', 'dog', 'rabbit', 'guinea pig'}
# Deleting 'fish' element
animal.remove('fish')
# Updated animal
print('Updated animal set:', animal)
Output
Traceback (most recent call last):
File "", line 5, in
animal.remove('fish')
KeyError: 'fish'
How do I remove a set in Python?
To remove an element from a set in python, users can utilize the remove(). This function removes the specified element and updates the set. If the element is not present inside the set, then an exception is thrown.
For example:
Source Code 1: When the element is present in the set
def Remove(sets11):
sets.remove("ash")
print (sets11)
# Driver Code
sets11 = set(["rambo", "ash", "karthik", "andy", "priya"])
Remove(sets11)
Output:
{"rambo", "karthik", "andy", "priya"}
Source Code 2: When the element is not present in the set
def Remove(sets11):
sets11.remove("gaurav")
print (sets11)
# Driver Code
sets11 = set(["rambo", "ash", "karthik", "andy", "priya"])
Remove(sets11)
Output:
No Output
Error:
Traceback (most recent call last):
File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 9, in
Remove(sets)
File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 4, in Remove
sets.remove("gaurav")
KeyError: 'gaurav'
How do you remove an element from a set?
Elements from a set in Python can be removed using two methods.
Method 1: Using the remove()
This method removes the element and returns an exception if the specified element is not present inside the set.
Method 2: Using the discard function
This method removes the element, and no exception is returned if the specified element is not present inside the set.
What is remove () in Python?
Remove() in Python is used to remove the specified list mentioned by the user.
For example:
fruits1 = {"apple", "orange", "banana", "mango", "cherry"}
fruits1.remove("banana")
print(fruits1)
Output:
{"apple", "orange", "mango", "cherry"}
How do you clear a set?
In python, the clear() is an in-built function used to remove all the elements of the set. This function makes the size of the set zero.
Syntax:
setname.clear()
Source Code:
int main()
{
set myset{ 1, 2, 3, 4, 5 };
myset.clear();
// Set becomes empty
// Printing the Set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
No Output
Leave a Reply