The pop() method in Python removes a random element from a given set and returns the removed element.
Set pop() method Syntax
set.pop()
Parameter: This method doesn’t take any parameter.
Return Value: It returns the removed element of the Set.
If you call this method on an empty set then an error is raised.
Python Set pop() method Example
In the following example we have a Set X
and we are calling pop() method on this set. The pop() method removes a random element from the set and returns the removed element.
# My Set X ={"hello", 2, 10, 99, "hi"} # Displaying Set before calling pop() print("Original Set is:",X) # Calling pop() method print("Removed element:",X.pop()) # Displaying Set after calling pop() print("Set after pop():", X)
Output:
Leave a Reply