The pop() function in python helps to remove the arbitrary element from the set. It also returns the removed element as its ouput.
set.pop()
The pop() function doesn't take any parameter. It updates the set by removing the return element.
The return value of this function is a random element from the set. This function raises a TypeError exception if the set is empty.
Input | Return Value |
---|---|
If set | Random element |
X ={1, 2, 3, 4}
print('Return Value is', X.pop())
print('X = ', X)
Output:
Return Value is 4 A = {1, 2, 3}
# on an empty set
X = {}
# Popping elements and printing them
print(X.pop())
# The updated set
print("Updated set is", X)
Output:
Traceback (most recent call last): File "/home/7c5b1d5728eb9aa0e63b1d70ee5c410e.py", line 6, in print(X.pop()) TypeError: pop expected at least 1 arguments, got 0