Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python Set pop() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Python set pop() Method removes any a random element from the set and returns the removed element.

Python set pop() Method Syntax:

Syntax: set_obj.pop()

Parameter: set.pop() doesn’t take any parameter.

Return: Returns the popped element from the set

Python set pop() Method Example:

Python3




s1 = {9, 1, 0}
s1.pop()
print(s1)

Output:

{9, 1}

Example 1: Basic usage of Python Set pop() Method

Python3




s1 = {1, 2, 3, 4}
s1.pop()
s1.pop()
s1.pop()
  
print("After 3 elements popped, s1:", s1)

Output:

After 3 elements popped, s1: {4}

Exceptions while using Python Set pop() Method

If the set is empty, then a TypeError is returned as shown by the following program.

Python3




S = {}
  
# popping an element
print(S.pop())
  
print("Updated set is", S)

Output:

Traceback (most recent call last):
  File "/home/7c5b1d5728eb9aa0e63b1d70ee5c410e.py", line 6, in 
    print(S.pop())
TypeError: pop expected at least 1 arguments, got 0

My Personal Notes arrow_drop_up
Last Updated : 25 Aug, 2022
Like Article
Save Article
Similar Reads
Related Tutorials