Write a python program to remove and return an albitary set element RaisekeyError if the set is empty?
Answers
Explanation:
In this article, we will see how to remove an element in a set, using the discard() and remove() method. We will also learn the difference between the two methods, although both of them produce the same results.
Examples:
Input : set = ([10, 20, 26, 41, 54, 20])
Output : {41, 10, 26, 54}
Input : set = (["ram", "aakash", "kaushik", "anand", "prashant"])
Output : {'ram', 'prashant', 'kaushik', 'anand'}
Method 1: Use of discard() method
The built-in method, discard() in Python, removes the element from the set only if the element is present in the set. If the element is not present in the set, then no error or exception is raised and the original set is printed.
If the element is present in the set:
# Python program to remove random elements of choice
# Function to remove elements using discard()
def Remove(sets):
sets.discard(20)
print (sets)