Account
Categories

Python Set pop() Method


Definition

It removes any one element randomly from the set. The pop() method does not accept any parameters. If the set is empty, an error message appears.

Now you will clearly understand the method through the flowchart.

s = {11, 15, 30}

pop()  →  removes a random element

        │
        ▼

     (Randomly selected)
            ↓
         15   ❌ removed

           │
           ▼

Before → {11, 15, 30}
After  → {11, 30}

x = 15  (removed element)

Syntax:

set_name.pop()

Example:

s = {11, 15, 30}
x = s.pop()
print(x)
print(s)

Output:

Exercise

Create a set s = {7, 8, 9} and remove any element using pop().