Python set pop() method removes a random element from the set when it is called. The pop() method removes any item anywhere from the set.
Python Set pop
The set pop() is a built-in Python method that removes the random element from the set and returns the removed element. The pop() method does not pop from the back or front; it can pop any element from anywhere present in the set.
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 set’s name, 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.
Example
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 the value randomly. We have popped values twice and printed each time the updated sets.
Though my PC is popping values from the front, in your case, it might be different, and each time you 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 the pop() method on 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}
That’s it for this tutorial.
See also
Python set intersection_update()