Python Set – pop() Method

It is a method in python set that returns a random element from the current set. It modifies the given set removing the popped element (the element which is returned). It doesn’t take any parameter.

Syntax:
set.pop()

Also Read: Python Set – discard() Method

Example 1:

# Python code to understand pop() method in set
# www.codewindow.in

s = {"amazing", "code", "best"}
s.pop()
print(s)

Output:

{'amazing', 'best'}

Explanation:
Here the random element “code” has been removed from the parent set (s).

Example 2:

# Python code to understand pop() method in set
# www.codewindow.in

s = {5, 8, 4, 1, 5, 9}
s.pop()
print(s)

Output:

{4, 5, 8, 9}

Explanation:
Here the random element “1” has been removed from the parent set (s).

Example 3:

# Python code to understand pop() method in set
# www.codewindow.in

s = {"8", "7", "5"}
s.pop()
print(s)

Output:

{'7', '5'}

Explanation:
Here the random element “8” was removed from the parent set (s).


Follow Us

You Missed

Also Checkout