A Python set is a list of things that are not in any particular order. – element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means we can change them after they’ve been formed.
Unlike other Python sets, the elements of the set have no index, which means we can’t access any element of the set directly using the index. We may, however, print them all together or loop through the collection to get the list of elements.
Example:
Input:
givenset= {'This', 'is', 'BTechGeeks'} element='is'
Output:
{'BTechGeeks', 'this'}
Remove a element from the Set
There are several ways to remove a element from the set some of them are:
Method #1:Using discard() function
In Python, the built-in method discard() removes an element from a collection only if it is already present. If the element is missing from the list, no error or exception is thrown, and the original set is printed.
Implementing the discard() function if the element is present in the set:
# given set givenset = {'this', 'is', 'BTechGeeks'} # given element element = 'is' # removing element which is in set using discard function. givenset.discard(element) # printing the set print(givenset)
Output:
{'BTechGeeks', 'this'}
Implementing the discard() function if the element is not present in the set:
# given set givenset = {'this', 'is', 'BTechGeeks'} # given element element = 'hello' # removing element which is not in set using discard function. givenset.discard(element) # printing the set print(givenset)
Output:
{'is', 'this', 'BTechGeeks'}
Method #2:Using remove() function
In Python, the built-in method remove() removes an element from a set only if it is already present, similar to how the discard() method removes an element from a set. If the element is missing from the list, an error or exception is thrown.
Implementing the remove() function if the element is present in the set:
# given set givenset = {'this', 'is', 'BTechGeeks'} # given element element = 'is' # removing element which is in set using remove() function. givenset.remove(element) # printing the set print(givenset)
Output:
{'BTechGeeks', 'this'}
Implementing the remove() function if the element is not present in the set:
# given set givenset = {'this', 'is', 'BTechGeeks'} # given element element = 'hello' # removing element which is not in set using remove() function. givenset.remove(element) # printing the set print(givenset)
Output:
Traceback (most recent call last): File "/home/ebe45b71f9f465d547ea9cb944ebe071.py", line 6, in <module> givenset.remove(element) KeyError: 'hello'
Method #3:Using pop() function
This operation takes an arbitrary element from the set and returns it.
It throws a KeyError if there are no elements to delete.
Implementing the pop() function if the element is present in the set:
# given set givenset = {'this', 'is', 'BTechGeeks'} # removing random element which is in set using pop() function. givenset.pop() # printing the set print(givenset)
Output:
{'is', 'BTechGeeks'}
Related Programs:
- remove a key from dictionary in python del vs dict pop vs comprehension
- python set remove vs discard vs pop
- python how to copy a dictionary shallow copy vs deep copy
- python how to add an element in list append vs extend
- python set add vs update
- python how to compare strings ignore case regex is vs operator
- python list comprehension vs generator expression explained with examples