# Python Set pop() Method Example

**Python set pop() is an inbuilt method that removes a random element from the set when it is called. This means it does not pop from the back or front, and it can pop any element from anywhere present in the set. The pop() method removes any item anywhere from the set.**

**Python Set pop() Method**

Content Overview

The** set pop() method **removes the random item from the **set** and returns the removed item. Unlike the stack, a random element is popped off the **set**. This is one of the essential functions of the **set** and accepts no arguments. The return value is the popped element from the **set**.

**Syntax**

set.pop()

Here, the set is the name of the set, and the pop() method does not take any argument as a parameter.

**Return Value**

The set pop() method returns the value which is popped. The value is popped randomly.

If the set is empty, it returns a **TypeError **exception.

See the following code example.

# app.py # Declaring sets # Set of name name = {'Debasis', 'Shubh', 'Shouvik', 'Rohit', 'Debanjan'} # Set of roll numbers roll = {24, 25, 27, 36, 40} # printing the sets print("Before popping name set is: ", name) print("Before popping roll set is: ", roll) # Now we will pop value from both the sets print("Name which is popped: ", name.pop()) print("Roll which is popped: ", roll.pop()) # Printing the updated sets print("Updated name set is: ", name) print("Updated roll set is: ", roll) print("Name which is popped: ", name.pop()) print("Roll which is popped: ", roll.pop()) # Printing the updated sets print("Updated name set is: ", name) print("Updated roll set is: ", roll)

**Output**

Before popping name set is: {'Rohit', 'Shouvik', 'Debanjan', 'Debasis', 'Shubh'} Before popping roll set is: {36, 40, 24, 25, 27} Name which is popped: Rohit Roll which is popped: 36 Updated name set is: {'Shouvik', 'Debanjan', 'Debasis', 'Shubh'} Updated roll set is: {40, 24, 25, 27} Name which is popped: Shouvik Roll which is popped: 40 Updated name set is: {'Debanjan', 'Debasis', 'Shubh'} Updated roll set is: {24, 25, 27}

We can see here that, when we are printing the set, values are showing according to the given input data. They are showing randomly.

Similarly, when we are popping the value, it is popping value randomly. We have popped values twice and printed each time the updated sets.

Though in my PC, it is popping values from the front, in your case, it might be different, and each time you will run this program, you will get different answers. So please don’t be confused if your answer is different.

**Operate pop() method on empty set**

Let’s operate method the empty set.

# app.py # Declaring empty set # Set of name name = {} # Now we will pop value from both the sets print("Name which is popped: ", name.pop())

**Output**

python3 app.py Traceback (most recent call last): File "app.py", line 7, in <module> print("Name which is popped: ", name.pop()) TypeError: pop expected at least 1 arguments, got 0

**Operate pop() on Python mixed set**

Let’s declare a mixed set and use the set pop method to remove a random item from a set.

# app.py mixed_set = {'Stranger Things', 11, 'Rick and Morty', 21, 'The Witcher', 46, 'Education'} print('The Original Set : ', mixed_set) x = mixed_set.pop() print('\npop Item : ', x) print('The Set after pop: ', mixed_set) y = mixed_set.pop() print('\npop Item : ', y) print('The Set after pop: ', mixed_set)

**Output**

python3 app.py The Original Set : {'Education', 'Stranger Things', 11, 'The Witcher', 46, 'Rick and Morty', 21} pop Item : Education The Set after pop: {'Stranger Things', 11, 'The Witcher', 46, 'Rick and Morty', 21} pop Item : Stranger Things The Set after pop: {11, 'The Witcher', 46, 'Rick and Morty', 21}

Finally, Python Set pop() Method Example is over.

**See also**

Python set intersection_update()