In this article, we are going to learn about 4 methods to Remove element from Python Set, all are in-built methods of Python Set to Remove elements (remove, discard, pop, clear). If you are wondering, how to delete elements from the python Set here is the answer. You can use the Built-in functions to do this job. We will cover these functions in this article, so you know, what to do to delete elements from a set.
These are the Set inbuilt function of the set to remove elements from the Set.
- Remove(): Remove an element from Set.
- Discard(): Remove an element from Set
- POP(): Remove an arbitrary(random) element
- Clear() :Remove all elemnts
1. Python Set Remove()
It removes the given element from the Set. If a passed element does not exist in Set, It raises a KeyError.
Syntax
Set.remove(element)
Parameter
- Element: It takes a single required parameter, and indicates the element we want to delete.
Return Value
- It removes the given element from the Set and updates the set. It returns None (nothing).
How to Remove element From Python Set using Remove()
In those code examples, we are removing elements from the set using the remove method. just like delete we can add single or mutiple elements to set
first_set = {'evening','Hi',5,6,7,'Good'}
#removing element using remove() method
first_set.remove(5)
print('set after removing element :',first_set)
Output
set after removing element : {6, 7, 'evening', 'Good', 'Hi'}
Example: When delete not exist Element using Remove()
In this code example, we are removing an element that does not exist in Set. As we can see it throwing key error.
first_set = {'evening','Hi',5,6,7,'Good'}
#removing not exist element using remove() method
first_set.remove(10)
print('set after removing element :',first_set)
Output
Traceback (most recent call last):
File "main.py", line 5, in <module>
first_set.remove(10)
KeyError: 10
2. Set Discard() to Remove element from Python Set
The set. discard() removes the given element from the set. If an element does not exist it will not raise an error. Instead of remove(), we can use this to avoid an error.
Syntax
set.remove(ele)
Parameters
- Element: It takes a single element as an argument that we want to delete.
Return Values
It returns None and removes the given element from the SET and updates the Set.
Example: Remove an element From Set using Discard()
In this python program, we have removed the element 6 from the python set just passing the element 6 as a parameter to discard() method
fruit_Set = {'apple','kiwi',5,6,7,'Fig'}
#removing element using discard() method
fruit_Set.discard(6)
print('set after removing element :',fruit_Set)
Output
set after removing element : {5, 'kiwi', 7, 'apple', 'Fig'}
Code Example: Remove not exist Element
In this python program example, we are removing the element that does not exist in Set using the discard() method. The Set. Discard() method does not throw any exception.
fruit_Set = {'apple','kiwi',5,6,7,'Fig'}
#removing not exist element using discard() method
print(fruit_Set.discard(8))
print('set after removing element :',fruit_Set)
Output
None
set after removing element : {'kiwi', 5, 'apple', 6, 7, 'Fig'}
3. Python Set Pop()
It removes an arbitrary(random) element from the Set and returns deleted element. It does not take any argument.
Syntax
set.pop()
Parameter
- The pop method does not take any argument.
Return Value
It returned an arbitrary deleted element from the set and update the set. The element no longer exists in SET.
Note: If the set is empty, it raises TypeError Exception.
Example: How to Remove an element from Python with Pop()
The Pop() remove element 5,because it choose randomly.
fruit_Set = {'apple','kiwi',5,6,7,'Fig'}
#removing element using pop() method
print('deleted element : ',fruit_Set.pop())
print('set after removing element :',fruit_Set)
Output
Note: The output might be different because Pop() deletes the arbitrary ( random) element.
deleted element : 5
set after removing element : {6, 7, 'kiwi', 'apple', 'Fig'}
4. Set Clear()
The clear() in-built method of set removes all elements of the Set. It does not take any argument.
Syntax
set.clear()
Parameters
- The Python set clear method does not take any argument.
Return value
- It returns none(nothing)
Example: Remove all elements of Set using the Clear() method
We are checking the value return by clear() method, as we can see it is returning None. The Set is empty after using the clear() method.
fruit_Set = {'apple','kiwi',5,6,7,'Fig'}
#removing all elements of the set using the clear() set method
print(' value return by clear method : ',fruit_Set.clear())
print('set after clear elements :',fruit_Set)
Output
value return by clear method : None
set after clear elements : set()