Tutorial Study Image

Python pop()

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() 
 

pop() Parameters:

The pop() function doesn't take any parameter. It updates the set by removing the return element.

pop() Return Value

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 

Examples of pop() method in Python

Example 1:Working of pop() in Python Sets?



X ={1, 2, 3, 4}

print('Return Value is', X.pop())
print('X = ', X)
 

Output:


Return Value is 4
A =  {1, 2, 3}

Example 2: How pop() work with an empty set?




# 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